Home > other >  Laravel Eloquent: Relationships 2 Layers deep
Laravel Eloquent: Relationships 2 Layers deep

Time:11-27

I have 2 eloquent models set like this:

Forum:

    protected $fillable = [
        'title',
        'description',
        'icon_path',
        'parent_id',
        'display_order',
        'forum_is_open',
    ];

    public function mods()
    {  
        return $this->hasMany(ForumModerator::class);

    }

ForumModerator:

   protected $fillable = [
        'forum_id',
        'user_id',
    ];

    public function forum()
    {
        return $this->belongsTo(Forum::class, 'forum_id', 'id');

    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');

    }

And I'm getting the list of Forums like this:

    return Forum::where('forum_is_open', 1)
            ->with(['subForums', 'mods.user'])
            ->orderBy('display_order', 'ASC')
            ->get();

and this is what the result looks like:

{
            "id": 52,
            "title": "Quia eius dolorem est sunt.",
            "description": "Dolore eveniet unde autem debitis. Natus et error quaerat dolor minima.",
            "icon_path": null,
            "parent_id": null,
            "display_order": 1,
            "forum_is_open": 1,
            "created_at": "2022-11-23T22:34:45.000000Z",
            "updated_at": "2022-11-23T22:34:45.000000Z",
            "sub_forums": [],
            "mods": [
                {
                    "id": 1,
                    "forum_id": 52,
                    "user_id": 3,
                    "created_at": null,
                    "updated_at": null,
                    "user": {
                        "id": 3,
                        "username": "Monserrat McCullough",
                        "email": "[email protected]",
                        "display_name": "Vincenza Lebsack",
                        "signature": null,
                        "group_id": 1,
                        "post_count": 0,
                        "created_at": "2022-11-25T04:15:12.000000Z",
                        "updated_at": "2022-11-25T04:15:12.000000Z"
                    }
                },
                {
                    "id": 2,
                    "forum_id": 52,
                    "user_id": 16,
                    "created_at": null,
                    "updated_at": null,
                    "user": {
                        "id": 16,
                        "username": "Prof. Norberto Kohler Sr.",
                        "email": "[email protected]",
                        "display_name": "Edmund Hickle",
                        "signature": null,
                        "group_id": 1,
                        "post_count": 0,
                        "created_at": "2022-11-25T04:15:12.000000Z",
                        "updated_at": "2022-11-25T04:15:12.000000Z"
                    }
                }
            ]
        },

I'm trying to get the list of mods for each Forum that has a mod, but without the entire ForumModerator record. bascically just want the user object to be in the mods array.

How do I need to set the relation between Forum and ForumModerator models to achive that?

Thanks

looked into hasManyThrough() function but wasn't able to achive the desired outcome

CodePudding user response:

The easiest way is to probably set up a many-to-many relationship between the Forum and the User models using the forum_moderators table:

class Forum extends Model
{
    public function users()
    {
        return $this->belongsToMany(User::class, 'forum_moderators');
    }
}

Then you can simply eager load the users relationship.

Alternatively, you can always modify your response using API Resources.

  • Related