Laravel Nova Admin Panel
Introduction
Laravel Nova is a beautifully designed administration panel for Laravel applications. It provides a powerful tool for developers to build custom admin panels without writing boilerplate code. Nova supports resource management, actions, metrics, filters, and more.
With Nova, you can create professional admin panels in minutes rather than days. It integrates seamlessly with your existing Laravel models and relationships.
Installation
First, install Nova via Composer:
composer require laravel/nova
After installation, publish the Nova assets:
php artisan nova:install
php artisan migrate
Register Nova in your routes/web.php:
Nova::routes()
->withAuthenticationRoutes()
->withPasswordResetRoutes();
Creating Resources
Resources are the heart of Nova. They define how your Eloquent models are displayed and managed in the admin panel. Create a resource using Artisan:
php artisan nova:resource User
Define the resource fields:
class User extends Resource
{
public static $model = \App\Models\User::class;
public function fields(NovaRequest $request)
{
return [
ID::make()->sortable(),
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
Email::make('Email')
->sortable()
->rules('required', 'email'),
Password::make('Password')
->onlyOnForms()
->rules('required', 'min:8'),
BelongsToMany::make('Roles'),
DateTime::make('Created At')->onlyOnIndex(),
];
}
}
Actions
Actions allow you to perform tasks on one or more resources. Create an action:
php artisan nova:action ExportUsers
Define the action logic:
class ExportUsers extends Action
{
public function handle(ActionFields $fields, Collection $models)
{
$csv = CSV::make($models->toArray());
return $csv->download('users.csv');
}
public function fields()
{
return [
Boolean::make('Include Headers'),
];
}
}
Attach the action to your resource:
public function actions(NovaRequest $request)
{
return [
new ExportUsers,
];
}
Metrics
Metrics provide visual insights into your data. Create a metric:
php artisan nova:metric TotalUsers
Define the metric calculation:
class TotalUsers extends Metric
{
public function calculate(NovaRequest $request)
{
return $this->count($request, User::class);
}
public function ranges()
{
return [
30 => '30 Days',
60 => '60 Days',
365 => '365 Days',
'MTD' => 'Month To Date',
'QTD' => 'Quarter To Date',
];
}
}
Custom Fields
Create custom fields for specialized data entry needs:
class MarkdownField extends Field
{
public function component()
{
return 'markdown-field';
}
public function resolveAttribute($resource)
{
return $resource->{$this->attribute};
}
}
Use custom fields in your resources:
MarkdownField::make('Content')
->withMeta(['height' => 300]),
Filters
Filters allow users to narrow down resource lists. Create a filter:
php artisan nova:filter UserStatus
Define filter options:
class UserStatusFilter extends Filter
{
public function apply(NovaRequest $request, $query, $value)
{
return $query->where('status', $value);
}
public function options(NovaRequest $request)
{
return [
'Active' => 'active',
'Inactive' => 'inactive',
'Pending' => 'pending',
];
}
}
Summary
Laravel Nova provides a powerful and elegant way to build admin panels for Laravel applications. With its intuitive interface and extensive customization options, Nova makes creating custom admin panels fast and enjoyable.
For more information, check out our other tutorials on Laravel Livewire and Laravel Forge.