PHP code example of waad / laravel-generate-repository-api

1. Go to this page and download the library: Download waad/laravel-generate-repository-api 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/ */

    

waad / laravel-generate-repository-api example snippets


php artisan repo:permission Car                     // get default guard

php artisan repo:permission Car --guard=api            // with guard

php artisan repo:permission Car --guard=api,web       // with multi guard

or

php artisan repo:permission Car --guard="api, web"    // with multi guard
 artisan optimize 

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool 
     */
    public function create(User $user)
    {
        return $user->hasAnyRolePermissions(['Admin', 'category_create'], ['api']); // "Admin" Or "category_create"
    }

    // **************************************************************

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\Models\User  $user
     * @return \Illuminate\Auth\Access\Response|bool 
     */
    public function create(User $user)
    {
        return $user->hasAllRolePermissions(['Admin', 'category_create'], ['api']); // "Admin" And "category_create"
    }

bash
php artisan vendor:publish --provider="Waad\Repository\RepositoryServiceProvider" 
bash
'providers' => [
    // ...
    Spatie\Permission\PermissionServiceProvider::class,
];
bash
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
bash
php artisan optimize:clear
php artisan migrate
js
'mysql' => [
    // .....
    'strict' => false,   // make false if used search parameter
    // .....
],
bash
php artisan repo:model NameModel --a
bash
php artisan repo:model NameModel                // generating model, properties
bash
php artisan repo:model NameModel --force        // generating model, properties (override)
bash
php artisan repo:model NameModel --m            // generating model, properties, migration
bash
php artisan repo:model NameModel --c            // generating model, generating controller, interface, repository, Request forms
bash
php artisan repo:model NameModel --permission --guard=agent,user      // generating model, properties, add permissions to database with multi guard
bash
php artisan migrate
bash
php artisan repo:validation NameModel --ndto       // generate StoreRequestForm And UpdateRequestForm in Http/Requests/NameModel/....  without DTO
js

use App\Models\Car;
use App\Policies\CarPolicy;


class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        Car::class => CarPolicy::class,
    ];

    
    public function boot()
    {
        $this->registerPolicies();

        // use super admin that don't check any permission if was Super Admin
        // you can change that from `config/laravel-generate-repository-api.php`
        Gate::before(function ($user, $ability) {
            return $user->hasRole(config('laravel-generate-repository-api.super_admin')) ? true : null;
        });
    }
}
js
use App\Http\Requests\Pagination;

public function index(Pagination $pagination)
{
    return $this->CarRepository->index($pagination);
}
js
use App\Http\Requests\Unlimit;

public function index(Unlimit $unlimit)
{
    return $this->CarRepository->index($unlimit);
}
js
// in Controller
public function index(Pagination $pagination)
{
    return $this->CarRepository->index($pagination, $pagination->trash);
}
js
// in controller or repository

use App\DTO\Car\CarDto;

public function show(Car $car)
{
    $car = $this->CarRepository->show($driver)->load('driver');

    return response()->json(new CarDto($car->toArray()));
}