PHP code example of singlequote / laravel-api-resource

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

    

singlequote / laravel-api-resource example snippets


/*
  |--------------------------------------------------------------------------
  | User routes
  |--------------------------------------------------------------------------
 */
Route::apiResource('users', UserController::class)->only('index', 'store', 'show', 'update', 'destroy');

   use SingleQuote\LaravelApiResource\Service\ApiPolicyService;

    public function toArray(Request $request): array
    {
        return [
            // ...
            'policies' => ApiPolicyService::defaults($this->resource),
        ];
    }

'policies' => ApiPolicyService::defaults($this->resource, ['sendInvite', 'acceptInvite']),


class User extends Authenticatable
{
    public array $apiRelations = [
        'roles.permissions', //allows for users.roles.permissions
    ];

$query->withSum('articles', 'price');

class Product extends Model
{
    /**
     * @var array
     */
    public array $apiOrderBy = [
        'articles_sum_price',
    ];
bash
php artisan vendor:publish --tag=laravel-api-resource-config
bash
php artisan vendor:publish --tag=laravel-api-resource-stubs
bash
php artisan make:api-resource {model}
bash
php artisan make:api-resource User
javascript
axios.get('/api/users?limit=100')
javascript
axios.get(route('api.users.index', {
     where: {
        date_of_birth: {
           gt: "1995-01-31"
        } 
    }
}))
// /api/users?where[date_of_birth][gt]=1995-01-31
javascript
axios.get(route('api.users.index', {
     where: [
	      {
	          created_at: {
	              lte: "1995-01-31 23:59"
	          }
	      }, {
	          created_at: {
	              gte: "1995-01-31 00:00"
	          }
	      }
	  ],
}))
// /api/users?where[0][date_of_birth][lte]=1995-01-31 23:59&where[1][date_of_birth][gte]=1995-01-31 00:00