Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notes on working with Laravel Nova.

Laravel Nova

Docs

Issues

Packages

Themes

Tutorials

Installation

Require the Nova package in your composer.json and run the nova:install and migrate artisan commands:

php artisan nova:install
php artisan migrate

Login

Default path: http://localhost/nova

Create a new user

php artisan nova:user

Localization

Update the language setting in your config/app.php config file:

'locale' => 'de'

Copy translations from franzdumfart/laravel-nova-localizations to your repo:

php -r "copy('https://raw.githubusercontent.com/franzdumfart/laravel-nova-localizations/master/lang/de.json', './resources/lang/vendor/nova/de.json') || exit (1);"

Resources

Generate a new Nova resource from an existing model:

php artisan nova:resource Post

Resource Authorization

Generate a new Laravel policy for the Post model:

php artisan make:policy PostPolicy -m Post

After that, map the new policy to the model in app/Providers/AuthServiceProvider.php:

protected $policies = [
    'App\Post' => 'App\Policies\PostPolicy',
];

Restrict the index query to the current User

Modify the Nova resource index query in app/Nova/Post.php so that Users can only see their own Posts:

public static function indexQuery(NovaRequest $request, $query)
{
    return $query->where('user_id', $request->user()->id);
}

Search

Global Search

Remove the public static $title = 'title'; in app/Nova/Post.php to define a custom title property on the resource class to modify the global search results:

public function title()
{
    return $this->title . ' - ' . $this->category;
}
public function subtitle()
{
    return "Author: {$this->user->name}";
}

To exclude the resource from the global search add the $globallySearchabl property on the resource app/Nova/Post.php:

public static $globallySearchable = false;

Resource Search

An empty $search array in app/Nova/Post.php will remove the searchbar of the resource:

public static $search = [];

Filters

Generate a new filter using the available artisan command:

php artisan nova:filter PostPubliished

Attach the filter to a resource via the filters() method.

public function filters(Request $request)
{
    return [
        new PostPubliished()
    ];
}

Lenses

Lenses are custom views in the admin panel. Generate a new lens:

php artisan nova:lens MostViews

Attach the lens to a resource via the lenses() method.

Actions

Actions are custom tasks that can be performed on eloquent models. Generate a new action:

php artisan nova:action PublishPost

Attach the action to a resource via the actions() method.

Action Fields

Populate the fields() method to request additional information (in a prompt) before running an action. The data is available in the handle() method:

Action::message($fields->message);

Action Authorization

Customize to whom and where actions are available with the canSee() with canRun() resource methods:

public function actions(Request $request)
{
    return [
        (new PublishPost())->canSee(function ($request) {
            // Restrict to user with ID 1.
            return $request->user()->id === 1;
        })->canRun(function ($request, $post) {
            // Restrict to post with ID 1.
            return $post->id === 1;
        })
    ];
}

Action Log

Add the Actionable trait to the Post model to enable a detailled log of all performed actions:

class Post extends Model
{
    use Actionable;
}

Metrics

Value Metrics

php artisan nova:value PostCount

Trend Metrics

php artisan nova:trend PostsPerDay

Partition Metrics

php artisan nova:partition PostsPerCategory

Tools

php artisan nova:tool acme/nova-tool

This command prompts to:

  1. Would you like to install the tool's NPM dependencies? (yes/no)

  2. Would you like to compile the tool's assets? (yes/no)

  3. Would you like to update your Composer packages? (yes/no)

Cards

php artisan nova:card acme/nova-card

This command prompts to:

Would you like to install the card's NPM dependencies? (yes/no)

Would you like to compile the card's assets? (yes/no)

Would you like to update your Composer packages? (yes/no)

Fields

php artisan nova:field acme/nova-field

This command prompts to:

Would you like to install the field's NPM dependencies? (yes/no)

Would you like to compile the field's assets? (yes/no)

Would you like to update your Composer packages? (yes/no)

Themes

php artisan nova:theme acme/nova-theme

This command prompts to:

Would you like to update your Composer packages? (yes/no)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.