spatie/laravel-endpoint-resources
Abandoned package that adds controller/action-based URL links to Laravel API resources and collection meta. Includes traits to generate “show/edit/update/delete” item links and “index/create/store” collection links automatically.
Sometimes you want to add links not belonging to a specific controller. Then it is possible to add an action as an link. They look just like a standard Laravel action:
class OtherResource extends JsonResource
{
use HasLinks;
public function toArray($request)
{
return [
'links' => $this->links()->action([UsersController::class, 'create']),
];
}
}
The HTTP verb for the action will be resolved from the route in Laravel. Should you have an action with two verbs, then you can always specify the verb for a particular action:
class OtherResource extends JsonResource
{
use HasLinks;
public function toArray($request)
{
$user = Auth::user();
return [
'links' => $this->links()
->action([UsersController::class, 'update'], [], 'PUT'),
];
}
}
Of course, it is also possible to use this with collection links:
class UserResource extends JsonResource
{
use HasLinks;
public static function collection($resource)
{
return parent::collection($resource)->additional([
'meta' => [
'links' => self::collectionLinks(UsersController::class)
->action([UsersController::class, 'update'], [], 'PUT'),
],
]);
}
}
How can I help you explore Laravel packages today?