Eloquent - Eager Loading Relationship

眉间皱痕 提交于 2020-01-30 05:29:04

问题


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?


回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/41160888/eloquent-eager-loading-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!