Laravel Admin Panel
Pros & Cons of using Nova
Why
A client asked me to transform an Excel based dataset to a web based platform. The data should be editable by their employees and should also be consumable by their web app via an JSON API.
There where no requirements for the design of the admin panel beside some basic styling like colors and font styles. This is why I decided to give Laravel Nova a try.
Laravel Nova
Laravel Nova is an admin panel for Laravel apps. Its created by the Laravel team and fits perfect in the Laravel eco system. It provides an easy to use approach for creating CRUD functionality for your resources without writing one line of HTML/CSS.
Like in Laravel apps the integrated artisan commands for defining new resources makes the development process even faster. If you are familiar with the way Laravel works you will get up and running really quick.
Laravel Nova basicaly works by defining resources based on your Laravel models and defining the fields you wish to display and/or edit. Nova provides field types for the most commong use cases. Nova can also handle model relationships.
- create Nova resource for your Laravel model/resource
- add fields you wish to view/edit
- add validation rules
- add authorization based on policies
- add filters, lenses, actions
This is an example of a User Resource which will transformed to an admin UI by Laravel Nova.
// UserResource.php
namespace App\Nova;
use Illuminate\Http\Request;
use Illuminate\Validation\Rules\Password as RulesPassword;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Password;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Boolean;
class UserResource extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = \App\Models\User::class;
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'name';
/**
* The logical group associated with the resource.
*
* @var string
*/
public static $group = 'admin';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id', 'name', 'email',
];
/**
* Hide from global search.
*
* @var boolean
*/
public static $globallySearchable = false;
/**
* Get the displayable label of the resource.
*
* @return string
*/
public static function label()
{
return __('Users');
}
/**
* Get the displayable singular label of the resource.
*
* @return string
*/
public static function singularLabel()
{
return __('User');
}
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name', 'name')
->sortable()
->rules('required', 'max:255'),
Text::make('Email', 'email')
->sortable()
->rules('required', 'email', 'max:254')
->creationRules('unique:users,email')
->updateRules('unique:users,email,{{resourceId}}'),
Password::make('Password', 'password')
->onlyOnForms()
->creationRules('required', 'string', RulesPassword::min(8)->mixedCase()->symbols()->numbers()->uncompromised())
->updateRules('nullable', 'string', RulesPassword::min(8)->mixedCase()->symbols()->numbers()->uncompromised()),
Boolean::make('Admin', 'is_admin')
->default(false)
->rules('boolean'),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
Based on the resource code Nova adds a menu entry to the admin panel where you can access the user resource. The fields get displayed in a table which is sortable and searchable by default.
There are separate create, update and detail views where you can perform basic CRUD operations.
When to use Nova & Conclusion
Laravel Nova is a perfect fit if you need an admin panel for your Laravel application. Adding custom field types or altering the UI to a complete custom design can be challenging though.
You should consider to use Nova if you
- need an admin panel for your Laravel app
- have no hard requirements on design and look and feel
- have Vue.js skills if you would like to add custom cards, fields, filters ect.