spatie/laravel-resource-links
Abandoned package that adds action URLs to Laravel API resources. Generate per-item and collection links (show/edit/update/delete, index/create/store) from a controller or defined actions, so resources include ready-to-use endpoints without manual URL building.
When creating a single resource like UserResource::make($user) you not only want the links tied to that resource but also the collection links for that resource. In this case next to the show, edit, update and delete links you also want the index, create and store links in your resource.
This can be done by merging the collection links with the single resource links like so:
class UserResource extends JsonResource
{
use HasLinks;
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'links' => $this->links(UsersController::class)->withCollectionLinks(),
];
}
}
The UserResource in a response will now look like this:
{
"data":[
{
"id":1,
"name": "Ruben Van Assche",
"links": {
"show": "https://laravel.app/users/1",
"edit": "https://laravel.app/users/1/edit",
"update": "https://laravel.app/users/1",
"delete": "https://laravel.app/users/1",
"index": "https://laravel.app/users",
"create": "https://laravel.app/users/create",
"store": "https://laravel.app/users"
}
}
]
}
Calling withCollectionLinks on every resource can be a bit tedious. That's why when you include the Spatie\ResourceLinks\HasMeta we'll not only add the meta helper but also automatic link merging when you would make a single resource.
Let's have a look, now when creating a single resource like so:
UserResource::make($user);
You would get all the links: show, edit, update, delete, index, create and store. This will only work when making a single resource, collection resources will have their collection links in the meta section.
How can I help you explore Laravel packages today?