Eloquent - Eager Loading Relationship
<p>I'm trying to figure out how to eager load data from a related table. I have 2 models <code>Group</code> and <code>GroupTextPost</code>.</p>

<p><strong>Group.php</strong></p>

<pre><code>&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

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

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

    public function messages()
    {
        return $this-&gt;hasMany('\App\Models\GroupTextPost');
    }
}
</code></pre>

<p><strong>GroupTextPost.php</strong></p>

<pre><code>&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

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

    public function group()
    {
        return $this-&gt;belongsTo('\App\Models\Group');
    }
}
</code></pre>

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

<p>I've tried doing it like so:</p>

<pre><code>public function messages()
{
    return $this-&gt;hasMany('\App\Models\GroupTextPost')-&gt;with('user');
}
</code></pre>

<p>... and calling like so:</p>

<pre><code>$group = Group::find($groupID);
$group-&gt;messages[0]-&gt;firstname
</code></pre>

<p>But I get an error:</p>

<pre><code>Unhandled Exception: Call to undefined method Illuminate\Database\Query\Builder::firstname()
</code></pre>

<p>Is this possible to do with Eloquent?</p>