PHP code example of wkasunsampath / laravel-autocrud

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

    

wkasunsampath / laravel-autocrud example snippets




namespace App\Http\Autocruds;

use WKasunSampath\LaravelAutocrud\BaseCrud;

class User extends BaseCrud
{
    /**
     * Set whether CRUD operations are related to API or not.
     */
    protected bool $isApi = true;

    /**
     * Model instance which CRUD operations relate.
     */
    protected string $model = \App\Models\User::class;

    /**
     * Middlewares which are applied to all routes
     *
     * Ex: ['auth:sanctum']
     */
    public function commonMiddlewares(): array
    {
        return [];
    }
}



namespace App\Http\Autocruds;

use WKasunSampath\LaravelAutocrud\AutocrudRouter;

class Autocruds extends AutocrudRouter
{
    /**
     * Register all autocrud classes here.
     */
    protected array $autocruds = [
        \App\Http\Autocruds\User::class,
    ];
}

/**
 * Eager load relationships with index query
 */
public function indexEagerLoad(): array
{
    return ['locations'];
}

/**
 * Request class name for create requests
 */
public function createRequest(): string|null
{
    return "\App\UserModule\Requests\UserRequest.php";
}

/**
 * Do things before add data to the DB.
 */
public function beforeCreate(array $data): array
{
    if (array_key_exists('password', $data)) {
        $data['password'] = Hash::make($data['password']);
    }
    
    if (array_key_exists('profile_picture', $data)) {
        //Perform file saving here & get $fileName.
        $data['profile_picture'] = $fileName;
    }

    return $data;
}
bash
php artisan autocrud:create User
bash
php artisan autocrud:install
bash
php artisan autocrud:create Company/Office