Installation:
composer require encore/laravel-admin
php artisan admin:install
This publishes config, migrations, and assets, and sets up the default admin route (/admin).
First Use Case:
Create a CRUD interface for a User model in 3 steps:
php artisan admin:make User
app/Admin/Controllers/UserController.php to define fields:
public function fields()
{
return [
ID::make()->sortable(),
Text::make('name')->sortable(),
Email::make('email')->sortable(),
// ...
];
}
/admin/users.Key Files to Review:
config/admin.php (core settings)app/Admin/Controllers/ (custom controllers)resources/views/vendor/adminlte/ (custom templates)Model-Based CRUD:
php artisan admin:make ModelName to scaffold controllers.fields(), form(), title(), and listRow() methods in controllers.Field Types:
ID, Text, Email, Boolean, Image, Markdown.BelongsTo, HasOne, HasMany, BelongsToMany.FormField or Column classes.Form Customization:
public function form()
{
return [
Text::make('name')->rules('required|max:255'),
Password::make('password')->onlyShowWhenCreating(),
Select::make('role')->options(['admin' => 'Admin', 'user' => 'User']),
];
}
List Views:
public function list()
{
return $this->table->columns([
ID::make()->sortable(),
Text::make('name')->sortable()->searchable(),
Boolean::make('active')->sortable(),
]);
}
Actions:
public function action()
{
return Action::make('Delete')
->icon('fa fa-trash')
->ajax(['method' => 'DELETE'])
->confirm('Are you sure?');
}
Integration with Laravel:
can('manage-users', $user)).Authentication:
config/admin.php).AdminAuth or override AuthServiceProvider.API Endpoints:
php artisan admin:make User --api
/admin/api/users.Dynamic Fields:
Text::make('dynamic_field')->visible(function () {
return request()->input('show_dynamic');
});
Reusable Components:
class BaseController extends Controller
{
public function title()
{
return 'Base Title';
}
}
Event Handling:
creating, updating):
public function onCreating()
{
$this->model->setAttribute('created_by', auth()->id());
}
Custom Views:
resources/views/vendor/adminlte/.partials/form-field.blade.php.Multi-Tenancy:
public function scopeTenants($query)
{
return $query->where('tenant_id', auth()->tenant()->id);
}
Bulk Actions:
public function batchActions()
{
return [
Download::make()->name('download'),
Delete::make()->name('delete'),
];
}
Form Tabs:
public function form()
{
return [
Tab::make('Basic Info', function () {
return [
Text::make('name'),
Email::make('email'),
];
}),
Tab::make('Advanced', function () {
return [
Textarea::make('bio'),
];
}),
];
}
Caching Issues:
php artisan config:clear
config/admin.php during development:
'cache' => [
'enabled' => env('APP_DEBUG') === false,
],
Route Conflicts:
/admin doesn’t clash with other routes. Use middleware:
Route::group(['middleware' => 'admin'], function () {
Admin::routes();
});
Field Validation:
Text::make('name')->rules('required|unique:users,name,'.$this->rowId);
Relationship Loading:
public function list()
{
return $this->table->model()->with('author');
}
Asset Compilation:
npm run dev or npm run prod if CSS/JS assets fail to load.resources/assets/admin/.Permission Denied:
app/Providers/AdminServiceProvider.php:
Gate::define('access-admin', function ($user) {
return $user->isAdmin();
});
Model Events vs. Admin Events:
creating, saving) are not Laravel’s Eloquent events. Use:
public function onCreating()
{
// Admin-specific logic
}
Log Admin Events:
config/admin.php:
'debug' => env('APP_DEBUG'),
storage/logs/laravel-admin.log.Dump Data:
dd($this->model) in controller methods to inspect data.Check Request Payload:
handle() in controllers to log requests:
public function handle()
{
\Log::info('Request data:', request()->all());
parent::handle();
}
Database Queries:
DB::enableQueryLog() to inspect queries:
public function list()
{
DB::enableQueryLog();
$result = $this->table->columns([...]);
\Log::info(DB::getQueryLog());
return $result;
}
Custom Field Types:
FormField or Column:
class CustomField extends FormField
{
public function render()
{
return '<input type="text" value="' . $this->value . '">';
}
}
Custom Actions:
app/Admin/Actions/:
class ExportAction extends Action
{
public function __construct()
{
parent::__construct('Export');
}
public function handle()
{
// Custom logic
}
}
Custom Middleware:
app/Http/Kernel.php:
protected $adminMiddleware = [
\App\Http\Middleware\CheckAdminStatus::class,
];
Custom Auth:
app/Admin/Auth/AdminAuth.php:
public function authenticate()
{
if (auth()->check()) {
return true;
}
return false;
}
Custom Themes:
resources/views/vendor/adminlte/.Custom API Responses:
app/Admin/Controllers/ApiController.php:
public function apiIndex()
{
return $this->response->api()->paginate();
}
Disable Unused Features:
'features' => [
'menu.builder' => false, // Disable menu builder if not needed
'menu.i18n' => false,
],
Lazy Load Relationships:
load() for relationships in list():
$this->table->model()->with(['author' => function ($query) {
$query->select('id', 'name');
}]);
How can I help you explore Laravel packages today?