Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent - Eager Loading Relationship

I'm trying to figure out how to eager load data from a related table. I have 2 models Group and GroupTextPost.

Group.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Group extends Model
{
    protected $table = 'group';

    public function type()
    {
        return $this->hasOne('\App\Models\GroupType');
    }

    public function user()
    {
        return $this->belongsTo('\App\Models\User');
    }

    public function messages()
    {
        return $this->hasMany('\App\Models\GroupTextPost');
    }
}

GroupTextPost.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class GroupTextPost extends Model
{
    protected $table = 'group_text_post';

    public function user()
    {
        return $this->belongsTo('\App\Models\User');
    }

    public function group()
    {
        return $this->belongsTo('\App\Models\Group');
    }
}

What I'm trying to do is eager load the user when fetching group text posts so that when I pull the messages the user's name is included.

I've tried doing it like so:

public function messages()
{
    return $this->hasMany('\App\Models\GroupTextPost')->with('user');
}

... and calling like so:

$group = Group::find($groupID);
$group->messages[0]->firstname

But I get an error:

Unhandled Exception: Call to undefined method Illuminate\Database\Query\Builder::firstname()

Is this possible to do with Eloquent?

like image 607
BugHunterUK Avatar asked Dec 15 '16 09:12

BugHunterUK


3 Answers

You should not eager load directly on the relationship. You could eager load the user always on the GroupTextPost model.

GroupTextPost.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class GroupTextPost extends Model
{
    protected $table = 'group_text_post';

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    protected $with = ['user'];

    public function user()
    {
        return $this->belongsTo('\App\Models\User');
    }

    public function group()
    {
        return $this->belongsTo('\App\Models\Group');
    }
}

Or you could use Nested Eager Loading

$group = Group::with(['messages.user'])->find($groupID);
$group->messages[0]->user->firstname
like image 165
Bert van Hoekelen Avatar answered Sep 20 '22 01:09

Bert van Hoekelen


You have to access the user before accessing directly the firstname.

$group->messages[0]->user->firstname;

The way you wrote it means the message has a firstname.

like image 25
warang580 Avatar answered Sep 21 '22 01:09

warang580


Create new relationship in Message model:

public function user()
{
    return $this->belongsTo('\App\User');
}

Load both relations:

$group = Group::with('messages', 'messages.user')->find($groupID);

foreach ($group->messages as $message) {
    $message->user->firstname;
}

https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

like image 27
Alexey Mezenin Avatar answered Sep 20 '22 01:09

Alexey Mezenin