Skip to content

Instantly share code, notes, and snippets.

@hofmannsven
Last active February 27, 2023 10:08
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save hofmannsven/9964613096d6b371ff933fc3202a44ef to your computer and use it in GitHub Desktop.
Save hofmannsven/9964613096d6b371ff933fc3202a44ef to your computer and use it in GitHub Desktop.
Notes on working with Laravel Nova.

Docs

Issues

Packages

Themes

Tutorials

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);"

Generate a new Nova resource from an existing model:

php artisan nova:resource Post

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

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 = [];

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 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 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.

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);

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;
        })
    ];
}

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

class Post extends Model
{
    use Actionable;
}
php artisan nova:value PostCount
php artisan nova:trend PostsPerDay
php artisan nova:partition PostsPerCategory
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)

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)

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