PHP code example of sirgrimorum / crudgenerator

1. Go to this page and download the library: Download sirgrimorum/crudgenerator 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/ */

    

sirgrimorum / crudgenerator example snippets


// config/sirgrimorum/crudgenerator.php
'admin_routes' => [
    'post' => 'config/sirgrimorum/models/post',
],

use App\Models\Post;

return [
    'modelo' => Post::class,
    'tabla'  => 'posts',
    'nombre' => 'title',    // Field used as the display name
    'id'     => 'id',
    'campos' => [
        'title' => [
            'tipo'  => 'text',
            'label' => 'Title',
        ],
        'body' => [
            'tipo'  => 'html',
            'label' => 'Body',
        ],
        'published_at' => [
            'tipo'  => 'datetime',
            'label' => 'Published At',
        ],
    ],
];

'status' => [
    'tipo'          => 'select',
    'label'         => 'Status',
    'placeholder'   => 'Choose a status',
    'description'   => 'Shown below the label',
    'help'          => 'Shown below the input',
    'valor'         => 'draft',              // Default value (or callable)
    'opciones'      => [                     // Options for select/checkbox/radio
        'draft'     => 'Draft',
        'published' => 'Published',
        'archived'  => 'Archived',
    ],
    'hide'          => ['list'],             // Hide in these views: create, edit, show, list
    'conditional'   => ['type' => 'post'],  // Only show when 'type' field = 'post'
    'enlace'        => "__route__posts.show,:modelId",  // Wrap list value in a link
    'truncate'      => 100,                  // Truncate text in list view
    'extraClassDiv'   => 'col-md-6',
    'extraClassInput' => 'form-control-sm',
    'tipos_temporales' => [
        'create' => 'hidden',                // Different type when creating
    ],
],

'author_id' => [
    'tipo'     => 'relationship',
    'label'    => 'Author',
    'modelo'   => App\Models\User::class,
    'campo'    => 'name',                   // Display field on related model
    'id'       => 'id',
    'nombre'   => 'name',
    'todos'    => 'all',                    // 'all', callable, or query builder
],

'photo' => [
    'tipo'   => 'file',
    'label'  => 'Photo',
    'path'   => 'uploads/posts/',
    'disk'   => 'public',
    'resize' => [
        'width'   => 800,
        'height'  => 600,
        'path'    => 'uploads/posts/thumbs/',
        'quality' => 85,
    ],
],

'metadata' => [
    'tipo'  => 'json',
    'label' => 'Metadata',
],

class Post extends Model
{
    public static $rules = [
        'title' => 'ug',
    ];
}

public static $error_messages = [
    'title.

CrudGenerator::lists(
    array $config,
    bool  $modales   = false,   // Enable Bootstrap modal buttons
    bool  $simple    = false,   // Minimal render (no JS/CSS 

CrudGenerator::create(array $config, bool $simple = false, bool $botonModal = false): string

CrudGenerator::edit(
    array $config,
    mixed $id        = null,
    bool  $simple    = false,
    mixed $registro  = null,
    bool  $botonModal = false
): string

CrudGenerator::show(array $config, mixed $id = null, bool $simple = false, mixed $registro = null): string

CrudGenerator::validateModel(array $config, \Illuminate\Http\Request $request): \Illuminate\Validation\Validator|false

CrudGenerator::saveObjeto(array $config, \Illuminate\Http\Request $request, mixed $registro = null): Model

CrudGenerator::getConfigWithParametros(string $modelo): array
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Sirgrimorum\CrudGenerator\CrudGeneratorServiceProvider" --tag=config
bash
php artisan vendor:publish --provider="Sirgrimorum\CrudGenerator\CrudGeneratorServiceProvider" --tag=lang
bash
php artisan vendor:publish --provider="Sirgrimorum\CrudGenerator\CrudGeneratorServiceProvider" --tag=views
bash
php artisan crudgen:resources
bash
php artisan crudgen:createconfig Post
blade
{{-- List with DataTables --}}
{!! CrudGenerator::lists($config, true) !!}

{{-- Create form --}}
{!! CrudGenerator::create($config) !!}

{{-- Edit form --}}
{!! CrudGenerator::edit($config, $id) !!}

{{-- Show (read-only) --}}
{!! CrudGenerator::show($config, $id) !!}