Skip to content
Advertisement

Laravel 5 Eloquent appending relationships to JSON on multiple levels

So it is pretty easy to include relationships on models such as:

class User extends Model {
     protected $with=['roles']
}

class Role extends Model {
     protected $with=['permissions']
}

when there’s a get request to the user resource, it will include associated roles automagically.

But in this setup, the role resources returned with user resource also includes it’s own included relationships like:

{user:{id:1, roles:[{id:1, permissions:[{id:1..

This generates huge objects, that mostly include unnecessary related child models.

I can workaround this by setting attributes to replace the default relationship includes but the API i am working on has 30+ resources and that path isn’t the ideal beause it will require me to write a lot of repeated code on models.

is there a way to manage the depth of appended relationships easily?

i imagine something like:

class Role extends Model {
     protected $with=['permissions'];
     protected $includeWith=[]; // role wont have the permissions appended when included
}

Advertisement

Answer

If somebody is still interested in a solution for this: Laravel 5.5 introduced Api Resources which is a great way to organize the api responses. Just don’t include any relationships on your model and append them on the resource.

namespace AppHttpResources;

use IlluminateHttpResourcesJsonResource;

class UserResource extends Resource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'roles'=> $this->roles

        ];
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement