PHP code example of kha333n / crudmodule

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

    

kha333n / crudmodule example snippets


use Kha333n\Crudable\Crudable;

class YourModel extends Model implements CrudableInterface
{
    use Crudable;
    
    public function crudable(): array {
        return [
            'column1',
            'column2',
            '...'
        ];
    }
}

// Create new record
$model = Book::getRepository()->create($request->all());

// Update record
$model->repository()->update($request->all());

// Delete record
$model->repository()->delete();

// Get all records
$models = Book::getRepository()->all();
//OR
$models = $model->repository()->all();

// force delete record
$model->repository()->forceDelete();

// in YourModel.php add array

    public array $rules = [
        'column1' => '];


// Only can create implemented
class Book implements \kha333n\crudmodule\Contracts\CrudableInterface {
    use \kha333n\crudmodule\Traits\CrudableAdapter;
    use \kha333n\crudmodule\Traits\Crudable;
    
    public function crudable(): array {
        return [
            'column1',
            'column2',
            '...'
        ];
    }
    
    public function canCreate(\Illuminate\Database\Eloquent\Model $model): bool {
        return auth()->user()->hasRole('Author');
    }
}

// See spatie query builder package for rules
    public function filters()
    {
        return $this->getFillable();
    }

    public function sorts()
    {
        return $this->getFillable();
    }

YourModel::getRepository()->all(new \kha333n\crudmodule\Structures\CrudConfiguration(
    withTrashed: true,
    onlyTrashed: true, // When onlyTrashed is true, it overrides, the withTrashed and only deleted record will show
    paginate: true,
    perPage: 23
))

    protected $listen = [
        'BookCreated' => [
            BookCreatedListener::class,
        ],
        'BookUpdated' => [
            BookUpdatedListener::class,
        ],
        'BookDeleted' => [
            BookDeletedListener::class,
        ],
        'BookForceDeleted' => [
            BookForceDeletedListener::class,
        ],
        'BookRestored' => [
            BookRestoredListener::class,
        ],
    ];

// BookCreatedListener.php
class BookCreatedListener
{
    public function handle(object $event)
    {
        // $event will contain the model instance on which operation performed
        // Perform your task here
    }
}