Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Endpoint Resources Laravel Package

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.

View on GitHub
Deep Wiki
Context7

title: Merging links weight: 5

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"
         }
      }
   ]
}

Automatically merge collection links

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai