PHPackages                             gdebrauwer/laravel-hateoas - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. gdebrauwer/laravel-hateoas

ActiveLibrary[API Development](/categories/api)

gdebrauwer/laravel-hateoas
==========================

Expose the authorization logic of your REST API using HATEOAS links on your Laravel API resources

2.4.0(10mo ago)17389.4k↑62.8%12[3 issues](https://github.com/gdebrauwer/laravel-hateoas/issues)[1 PRs](https://github.com/gdebrauwer/laravel-hateoas/pulls)MITPHPPHP ^8.0|^8.1|^8.2|^8.3|^8.4CI passing

Since Aug 17Pushed 10mo ago3 watchersCompare

[ Source](https://github.com/gdebrauwer/laravel-hateoas)[ Packagist](https://packagist.org/packages/gdebrauwer/laravel-hateoas)[ Docs](https://github.com/gdebrauwer/laravel-hateoas)[ RSS](/packages/gdebrauwer-laravel-hateoas/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (23)Used By (0)

Laravel HATEOAS
===============

[](#laravel-hateoas)

[![Latest Version on Packagist](https://camo.githubusercontent.com/184b9bb3a26508d0381bf4a2d12dc89255697cdb760c4bd05cd232b5e100448f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f676465627261757765722f6c61726176656c2d686174656f61732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gdebrauwer/laravel-hateoas)[![Total Downloads](https://camo.githubusercontent.com/9faf27c8e70babb248e4a2a81871a906602953cebf285cb441b41f1fe888a0a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f676465627261757765722f6c61726176656c2d686174656f61732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gdebrauwer/laravel-hateoas)

[HATEOAS](https://en.wikipedia.org/wiki/HATEOAS) allows you to expose the authorization logic of your REST API. This package makes it easy to add HATEOAS links to your Laravel API resources.

Each resource has its HATEOAS links, and only the accessible links per resource are returned. If a link is not available on a resource, then the clients of your API can disable functionality linked to that HATEOAS link.

By default an array of links, in the following format, will be added to the JSON of a Laravel API resource:

```
{
    "data": [
        {
            "id": 1,
            "text": "Hello world!",
            "_links": [
                {
                    "rel": "self",
                    "type": "GET",
                    "href": "http://localhost/message/1"
                },
                {
                    "rel": "delete",
                    "type": "DELETE",
                    "href": "http://localhost/message/1"
                }
            ]
        }
    ]
}
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require gdebrauwer/laravel-hateoas
```

Usage
-----

[](#usage)

You can create a new HATEOAS class for a model using the following artisan command:

```
php artisan make:hateoas MessageHateoas --model=Message
```

In the created class you can define public methods that will be used to generate the links. A method should either return a link or `null`.

```
class MessageHateoas
{
    use CreatesLinks;

    public function self(Message $message) : ?Link
    {
        if (! auth()->user()->can('view', $message)) {
            return;
        }

        return $this->link('message.show', ['message' => $message]);
    }

    public function delete(Message $message) : ?Link
    {
        if (! auth()->user()->can('delete', $message)) {
            return $this->link('message.archive', ['message' => $message]);
        }

        return $this->link('message.destroy', ['message' => $message]);
    }
}
```

To add the links to an API resource, you have to add the `HasLinks` trait and use the `$this->links()` method. The HATEOAS class will be automatically discovered.

```
class MessageResource extends JsonResource
{
    use HasLinks;

    public function toArray($request) : array
    {
        return [
            'id' => $this->id,
            'text' => $this->text,
            '_links' => $this->links(),
        ];
    }
}
```

Customization
-------------

[](#customization)

#### Formatting

[](#formatting)

You can customize the JSON links formatting by providing a formatter class that implements the `Formatter` interface to the `formatLinksUsing` method. If the code to format the links is pretty small or you don't want to create a separate formatter class for it, you also have the option to provide a formatting callback function to the `formatLinksUsing` method.

```
use GDebrauwer\Hateoas\Hateoas;
use GDebrauwer\Hateoas\LinkCollection;

// Provide your own Formatter class ...
Hateoas::formatLinksUsing(CustomFormatter::class);

// ... Or provide a callback
Hateoas::formatLinksUsing(function (LinkCollection $links) {
    // return array based on links
});
```

#### HATEOAS class discovery

[](#hateoas-class-discovery)

By default, the HATEOAS classes of models will be auto-discovered. Specifically, the HATEOAS classes must be in a Hateoas directory below the directory that contains the models. If you would like to provide your own HATEOAS class discovery logic, you can register a custom callback:

```
use GDebrauwer\Hateoas\Hateoas;

Hateoas::guessHateoasClassNameUsing(function (string $class) {
    // return a HATEOAS class name
});
```

Testing
-------

[](#testing)

```
composer test
```

Linting
-------

[](#linting)

```
composer lint
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Günther Debrauwer](https://github.com/gdebrauwer)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance54

Moderate activity, may be stable

Popularity48

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity85

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 95.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~108 days

Recently: every ~308 days

Total

21

Last Release

308d ago

Major Versions

1.8.0 → 2.0.02022-02-27

PHP version history (8 changes)1.0.0PHP ^7.2

1.4.0PHP ^7.3

1.6.0PHP ^7.3|^8.0

v1.x-devPHP ^7.3|^8.0|^8.1

2.0.0PHP ^8.0|^8.1

2.1.0PHP ^8.0|^8.1|^8.2

2.2.0PHP ^8.0|^8.1|^8.2|^8.3

2.3.0PHP ^8.0|^8.1|^8.2|^8.3|^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/739571b3160077a91cf01209a045409a0daf361b73fae482f37dfcf1db301a57?d=identicon)[gdebrauwer](/maintainers/gdebrauwer)

---

Top Contributors

[![gdebrauwer](https://avatars.githubusercontent.com/u/22586858?v=4)](https://github.com/gdebrauwer "gdebrauwer (156 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (6 commits)")[![JamesMcClelland](https://avatars.githubusercontent.com/u/10724551?v=4)](https://github.com/JamesMcClelland "JamesMcClelland (1 commits)")

---

Tags

apihateoaslaravelresourcesrest-apiapilaravelREST APIHATEOASgdebrauwer

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/gdebrauwer-laravel-hateoas/health.svg)

```
[![Health](https://phpackages.com/badges/gdebrauwer-laravel-hateoas/health.svg)](https://phpackages.com/packages/gdebrauwer-laravel-hateoas)
```

###  Alternatives

[mpociot/laravel-apidoc-generator

Generate beautiful API documentation from your Laravel application

3.5k3.1M12](/packages/mpociot-laravel-apidoc-generator)[andreaselia/laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API

1.0k586.2k3](/packages/andreaselia-laravel-api-to-postman)[api-ecosystem-for-laravel/dingo-api

A RESTful API package for the Laravel and Lumen frameworks.

3121.5M10](/packages/api-ecosystem-for-laravel-dingo-api)[mollie/laravel-mollie

Mollie API client wrapper for Laravel &amp; Mollie Connect provider for Laravel Socialite

3624.1M28](/packages/mollie-laravel-mollie)[shahghasiadil/laravel-api-versioning

Elegant attribute-based API versioning solution for Laravel applications with built-in deprecation management and version inheritance

2913.6k](/packages/shahghasiadil-laravel-api-versioning)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
