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 Resource Links Laravel Package

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.

View on GitHub
Deep Wiki
Context7

title: Resource setup weight: 1

In your resources, add the HasLinks trait and a new key where the links will be stored:

use Spatie\ResourceLinks\HasLinks;

class UserResource extends JsonResource
{
    use HasLinks;

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'links' => $this->links(UsersController::class),
        ];
    }
}

Now every UserResource has an additional LinkResource which in the responses will look like:

{
    'id': 1,
    'name': 'Ruben'
    '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"
    }
}

By default, we'll only construct links from the show, edit, update and delete methods of your controller.

Collection links

What about links like index, create and store? These links are not tied to a single model instance, so it's not a good idea to store them at that level. Instead, it's better to put the links to those collection links on the collection level of a resource.

You can put the collection links in the meta section of a resource collection like so:

class UserResource extends JsonResource
{
    use HasLinks;

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'links' => $this->links(UsersController::class),
        ];
    }

    public static function collection($resource)
    {
        return parent::collection($resource)->additional([
            'meta' => [
                'links' => self::collectionLinks(UsersController::class)
             ],
         ]);
    }
}

Now when we create an UserResource collection, the meta section will look like this:

"meta": {
   "links": {
        "index": "https://laravel.app/users",
        "create": "https://laravel.app/users/create",
        "store":  "https://laravel.app/users"
   }
}

By default, the collection links will only be constructed for the index, create and store methods in your controller.

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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport