Basics
Defining Resources
By default, Rest resources are stored in the app/Rest/Resources directory of your application. You may generate a new resource using the rest:resource Artisan command:
php artisan rest:resource UserResource
You might now define the model property. This property tells Laravel Rest Api which Eloquent model the resource corresponds to:
public static $model = 'App\Models\User';
Freshly created Rest resources only contain an ID exposed field. Don't worry, we'll add more fields to our resource soon.
Registering Resources
By default, resources are not automatically registered to let you take advantage of the logic.
First, you need to declare a Controller:
php artisan rest:controller UsersController
Then, specify the resource in your controller:
* The resource the controller corresponds to.
*
* @var class-string<\Lomkit\Rest\Http\Resource>
*/
public static $resource = App\Rest\Resources\UserResource::class;
Since the basic usage of these will be on the api side, you can declare your controller in your api.php
file:
use \Lomkit\Rest\Facades\Rest;
Rest::resource('users', \App\Rest\Controllers\UsersController::class)
Once your resources are registered, you can verify that by using php artisan route:list
:
+-----------+-------------------------------------------------+----------------------------------------+
| Method | URI | Name |
+-----------+-------------------------------------------------+----------------------------------------+
| GET | api/users | api.users.details |
| POST | api/users/search | api.users.search |
| POST | api/users/actions/{action} | api.users.operate |
| POST | api/users/mutate | api.users.mutate |
| DELETE | api/users | api.users.destroy |
+-----------+-------------------------------------------------+----------------------------------------+
Soft Deletes
If you want to expose soft delete routes for one of your resources, you can achieve this by using the `withSoftDeletes' method while registering the resource:
use \Lomkit\Rest\Facades\Rest;
Rest::resource('users', \App\Rest\Controllers\UsersController::class)->withSoftDeletes()
You now have two more routes registered:
+-----------+-------------------------------------------------+----------------------------------------+
| Method | URI | Name |
+-----------+-------------------------------------------------+----------------------------------------+
| POST | api/users/restore | api.users.restore |
| DELETE | api/users/force | api.users.force |
+-----------+-------------------------------------------------+----------------------------------------+
If you don't want to expose all soft deletes routes you can specify so:
use \Lomkit\Rest\Facades\Rest;
Rest::resource('users', \App\Rest\Controllers\UsersController::class)->withSoftDeletes(['forceDelete', 'restore'])