fbpx

Vander Host Favourite Laravel Packages

Background

Laravel Nova  is one of the most powerful web development toolkits if you’re a Website or PHP/Laravel developer. For a rock bottom once off price of $99 you can save years in development time by simply purchasing this software. At Vander Host, most of our back-end systems are created using Laravel and Nova. As such, we need a quick way to bootstrap new applications and this article is just what we need to get into the hyperlane.

Updated 7 November 2021 to include boilerplate installation and composer require commands:

Installation

laravel new project-name --jet
cd project-name

mc -e .env, add database, fix email sending to be Helo compatible

MAIL_HOST=localhost
MAIL_USERNAME="Voice-Commander"
MAIL_FROM_ADDRESS="[email protected]"

We install Nova via composer since we have a license and this method allows auto updating.

<span class="token function">composer</span> config repositories.nova <span class="token string">'{"type": "composer", "url": "https://nova.laravel.com"}'</span> --file composer.json

Then update composer.json

<span class="token property">"require"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">"php"</span><span class="token operator">:</span> <span class="token string">"^7.2.5"</span><span class="token punctuation">,</span>
    <span class="token property">"fideloper/proxy"</span><span class="token operator">:</span> <span class="token string">"^4.2"</span><span class="token punctuation">,</span>
    <span class="token property">"laravel/framework"</span><span class="token operator">:</span> <span class="token string">"^7.0"</span><span class="token punctuation">,</span>
    <span class="token property">"laravel/nova"</span><span class="token operator">:</span> <span class="token string">"~3.0"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token function">composer</span> update --prefer-dist
php artisan nova:install
php artisan migrate

Then the fun continues!

composer require gregoriohc/laravel-nova-theme-responsive
php artisan vendor:publish --provider="Gregoriohc\LaravelNovaThemeResponsive\ThemeServiceProvider"
composer require chris-ware/nova-breadcrumbs

In tools:

\ChrisWare\NovaBreadcrumbs\NovaBreadcrumbs::make(),

In resource:

use Breadcrumbs;

While in resource replace indexQuery:

/**
* Default ordering for custom index query.
*
* @var array
*/

public static $sort = ['id' => 'desc'];

/**
* Custom index query that allows easy sorting of individual
* resources by simply adding a public static $sort option
*/

public static function indexQuery(NovaRequest$request, $query)
{
   if (empty($request->get('orderBy'))) {
      $query->getQuery()->orders = [];
      return $query->orderBy(key(static::$sort), reset(static::$sort));
   }

   return $query;
}

You’ll need this later in individual resources:

public static $sort = ['id' => 'desc'];

In config/app.php change timezone and locale

In config/nova.php change path and currency

php artisan nova:user
composer require david-griffiths/nova-dark-theme
php artisan nova-dark-theme:add-switch

Create a model but at the same time also create a migration:

php artisan make:model Command -m

Create your first ENUM. Programming is all about consistency. To keep databases values consistent (and in lowercase), but still have a refactorable UI, we use ENUMs. Once you do one, you can do 100.

<?php

namespace App\Enums;

class CommandStatus
{
   const NEW = "new";
   const IDEA = "idea";
   const DRAFT = "draft";
   const PRODUCTION = "production";
   
   public static function uiOptions()
   {
      return [
         self::NEW => 'New',
         self::IDEA => 'Idea',
         self::DRAFT => 'Draft',
         self::PRODUCTION => 'Production',
      ];
   }
}

Expand your users table with these two fields:

$table->string('api_token')->nullable();
$table->boolean('admin')->false();

You’ll need a factory to create model examples and for testing. Have a look at the UserFactory, but here’s the command:

php artisan make:factory CommandFactory -m Command

Here’s an example of a custom Seeder (here’s the docs). Here’s the inside:

protected $model = Command::class;

public function definition()
{
   return [
      'name' => $this->faker->name,
   ];
}

Here’s a nice way of calling related records:

Category::factory(3)
->has(Command::factory()->count(3))
->create();

You’ll need log file troubleshooting:

composer require kabbouchi/nova-logs-tool

In tools:

new \KABBOUCHI\LogsTool\LogsTool(),

Publish:

php artisan vendor:publish --provider="KABBOUCHI\LogsTool\LogsToolServiceProvider"

Edit nova-logs-tools.php and specify 25

BelongsTo Create Related Record:

BelongsTo::make('Lead Source')
->showCreateRelationButton(),

You want compact table styles / columns by default. In the base resource class:

public static $tableStyle = 'tight';
You probably want to clear the footer and logo. Just search footer.php and logo.php and comment the code. While you’re doing cleanup, also remove the ‘help’ card in the Nova service provider or comment it. To be really fancy, you can publish the help card in the right folder and then modify it! Instructions will follow.
Next you probably want to output ENUMs using the fancy uiOptions() static method:
Select::make('Status')
->options(CommandStatus::uiOptions())
->displayUsingLabels(),
And if you want something pretty:
Badge::make('Status')->map([
   'production' => 'success',
   'draft' => 'info',
   'idea' => 'warning',
   'new' => 'danger',
])->sortable(),
Here’s the coolest notes plugin. Please note have you have to publish migration for Multi Tenancy.
composer require optimistdigital/nova-notes-field
php artisan migrate
php artisan vendor:publish --provider="OptimistDigital\NovaNotesField\NotesFieldServiceProvider" --tag="config"
php artisan vendor:publish --provider="OptimistDigital\NovaNotesField\NotesFieldServiceProvider" --tag="migrations"
To use it on Model, add use HasNotes and in resource NotesField
Don’t like the default alphabetical ordering of resources? Read this or do this:
In NovaServiceProvider register() function
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">register</span>()
    </span>{
        Nova::sortResourcesBy(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"><span class="hljs-variable">$resource</span></span>) </span>{
            <span class="hljs-keyword">return</span> <span class="hljs-variable">$resource</span>::<span class="hljs-variable">$priority</span> ?? <span class="hljs-number">99999</span>;
        });
    }
In resource:
public static $priority = 1;
If you don’t want the “With Trashed”
->withoutTrashed()

Old Article with Descriptions

Here are some of our favourite Laravel packages for bootstrapping successful projects:

Share this article

Share on facebook
Share on twitter
Share on linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top