PHP code example of vmorozov / laravel_admin_generator

1. Go to this page and download the library: Download vmorozov/laravel_admin_generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vmorozov / laravel_admin_generator example snippets




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);

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',
        ]
    ];



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,
        
                ],
    ];
}

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']);

}

public $adminFields = [
        'name' => [
            'searchable' => true,
        ],
    ];



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     
}