This package is made for speeding up development of admin panel for your Laravel project.
It helps you to handle common tasks in admin panel development for your project.
- Quick CRUD Generation
- Working with different field types
- Handling relationships
- Search
- Export Data to xls, csv file
- Using default Order By and Where conditions
- Ability to add additional buttons to each list item
- Files uploading to Model column
- Files uploading using spatie/laravel-medialibrary package
composer require vmorozov/laravel_admin_generator
php artisan vendor:publish
and selectVmorozov\LaravelAdminGenerator\AdminGeneratorServiceProvider
to publish all needed files for admin panel.
<?php
namespace App\Http\Controllers\Admin;
use App\Product;
use Vmorozov\LaravelAdminGenerator\App\Controllers\CrudController;
class ProductsController extends CrudController
{
protected $model = Product::class;
protected $url = 'products';
protected $titlePlural = 'Товары';
protected $titleSingular = 'Товар';
}
...
AdminRoute::resource(\App\Http\Controllers\Admin\ProductsController::class);
- text - for simple input type="text"
- textarea - for simple textarea
- number - for input type="number"
- email - for input type="email"
- date - for input type="date"
- datetime - for input type="datetime"
- file - for input type="file"
- select - for one-to-one relationship via select
- select_multiple - for many-to-many relationships via select multiple
public $adminFields = [
'name' => [
'label' => 'Name',
'displayInForm' => true,
'display_in_list' => true,
'searchable' => true,
'min' => 2,
'max' => 50,
],
'description' => [
'label' => 'Description',
'displayInForm' => true,
'display_in_list' => true,
'searchable' => false,
'min' => 2,
'max' => 5000,
],
'price' => [
'label' => 'Price',
'display_in_create_form' => true,
'display_in_update_form' => true,
'display_in_list' => true,
'field_type' => 'number',
'min' => 0,
'max' => 100000,
],
'user_id' => [
'label' => 'User Id',
'display_in_create_form' => true,
'display_in_update_form' => true,
'display_in_list' => true,
'min' => 0,
'field_type' => 'select',
'relation' => 'user',
'relation_model' => User::class,
'relation_display_attribute' => 'name',
],
'users' => [
'label' => 'Users Many To Many',
'display_in_create_form' => true,
'display_in_update_form' => true,
'min' => 0,
'field_type' => 'select_multiple',
'relation' => 'users',
'relation_model' => User::class,
'relation_display_attribute' => 'name',
],
'updated_at' => [
'display_in_create_form' => true,
'display_in_update_form' => true,
'display_in_list' => false,
'field_type' => 'date_time',
]
];
<?php
namespace App\Http\Controllers\Admin;
use App\Product;
use Vmorozov\LaravelAdminGenerator\App\Controllers\CrudController;
class ProductsController extends CrudController
{
protected $model = Product::class;
protected $url = 'products';
protected $titlePlural = 'Товары';
protected $titleSingular = 'Товар';
protected $columnParams = [
'name' => [
'label' => 'Name',
'display_in_create_form' => true,
'display_in_update_form' => true,
'display_in_list' => true,
'searchable' => true,
'min' => 2,
'max' => 50,
],
'description' => [
'label' => 'Description',
'display_in_create_form' => true,
'display_in_update_form' => true,
'display_in_list' => true,
'searchable' => false,
'min' => 2,
'max' => 5000,
],
];
}
Open file resources/views/vendor/vmorozov/laravel_admin_generator/layouts/sidebar.blade.php and add your links to the generated admin panel endpoints.
protected function setup()
{
$this->addDefaultWhereClause('password', '!=', null);
$this->addDefaultOrderByClause('id', 'desc');
}
protected function setup()
{
// without putting entity id to the url
$this->addListItemButton(url('/test_button'), 'test button');
// with putting entity id to the url
$this->addListItemButton(url('/test_button/{id}'), '<i class="fa fa-check" aria-hidden="true"></i> test button', 'btn btn-success', ['target' => '_blank']);
}
To add search functionality you should just add searchable param to the field setup
public $adminFields = [
'name' => [
'searchable' => true,
],
];
To start using this package in admin panel install package.
Than you should add additional setups to your model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
use Vmorozov\LaravelAdminGenerator\App\Utils\ModelTraits\AdminPanelTrait;
class Product extends Model implements HasMedia
{
use AdminPanelTrait;
use HasMediaTrait;
public $mediaCollections = [
'main_image' => [
'name' => 'Main image',
'single_file' => true
],
'gallery' => [
'name' => 'Gallery'
],
];
// Some other code here
}
- Simple collection for multiple files
- Collection for single file (Such as avatar image of user, main image of product, etc).
To make collection single use
'single_file' => true
parameter.