vmorozov / laravel_admin_generator
This package is made to speed up development of admin panel for your laravel project
Installs: 1 827
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 4
Open Issues: 2
Language:HTML
Requires
- php: >=7.2
- almasaeed2010/adminlte: 2.4.*
- illuminate/database: >=5.3
- illuminate/http: >=5.3
- illuminate/routing: >=5.3
- illuminate/support: >=5.3
- maatwebsite/excel: ^3.1
- spatie/laravel-medialibrary: >=7.1
- vmorozov/laravel-file-uploads: ^1.1
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: 3.*
- phpunit/phpunit: ^8.0
README
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.
Features
- 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
1. Installation
composer require vmorozov/laravel_admin_generator
php artisan vendor:publish
and selectVmorozov\LaravelAdminGenerator\AdminGeneratorServiceProvider
to publish all needed files for admin panel.
2. Setup
1. Create Controller
<?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 = 'Товар'; }
2. Add record to routes/admin.php
... AdminRoute::resource(\App\Http\Controllers\Admin\ProductsController::class);
3. (Optional) Setup fields
Available field types:
- 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
in Model
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', ] ];
In Controller
<?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, ], ]; }
3. Add link to the sidebar
Open file resources/views/vendor/vmorozov/laravel_admin_generator/layouts/sidebar.blade.php and add your links to the generated admin panel endpoints.
3. Advanced Usage
Add default order by and where clauses to the list query
protected function setup() { $this->addDefaultWhereClause('password', '!=', null); $this->addDefaultOrderByClause('id', 'desc'); }
Add additional button to each item in list
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']); }
Search
To add search functionality you should just add searchable param to the field setup
public $adminFields = [ 'name' => [ 'searchable' => true, ], ];
Working with spatie/laravel-medialibrary
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 }
There are tho kinds of available collections setups
- 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.