Validations
Keep in mind that if you want your API to provide the best user experience to your consumers you always want to have 0 direct database errors. For example, if one of your fields is not nullable, you'll want to make it required in these rules. You can specify all Laravel rules you want.
Rules
You may use the rules
method from your resource to specify the rules for your attributes.
public function rules(\Lomkit\Rest\Http\Requests\RestRequest $request) {
return [
'name' => 'required'
];
}
Create rules
You may use the createRules
method from your resource to specify the creating rules.
public function createRules(\Lomkit\Rest\Http\Requests\RestRequest $request) {
return [
'name' => 'required'
];
}
Update rules
You may use the updateRules
method from your resource to specify the updating rules.
public function updateRules(\Lomkit\Rest\Http\Requests\RestRequest $request) {
return [
'name' => 'required'
];
}
Relation pivot rules
In some particular cases, you might want to validate the pivot fields provided by the incoming request. You can achieve this with the withPivotRules
method when declaring your relationship:
use Lomkit\Rest\Relations\BelongsToMany;
BelongsToMany::make('roles', RoleResource::class)
->withPivotFields(['created_at'])
->withPivotRules([
'created_at' => ['required', 'date']
]),
Relationships
In addition to the variety of fields we've already discussed, Laravel Rest Api has full support for all of Laravel's relationships. After adding relationships to your REST resources, you'll be able to directly access them from the requested resource, eliminating the need for multiple API calls.
Interactions
Laravel Rest API offers the flexibility to customize certain aspects of its functionality, allowing you to tailor it to your specific needs and leverage its capabilities to your advantage.