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
delete links you also want the
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
public function toArray($request)
'id' => $this->id,
'name' => $this->name,
'links' => $this->links(UsersController::class)->withCollectionLinks(),
UserResource in a response will now look like this:
"name": "Ruben Van Assche",
Automatically merge collection links
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:
You would get all the links:
store. This will only work when making a single resource, collection resources will have their collection links in the meta section.