I have 2 models like this:
class Task extends Eloquent {
protected $guarded = ['id'];
public function user() {
return $this->belongsTo('User');
}
}
class User extends Eloquent {
protected $guarded = ['id'];
public function task() {
return $this->hasMany('Task');
}
}
..and a blade index section that looks like this:
@foreach ($tasks as $task)
{{ "<tr><td>$task->user->fullname</td></tr>" }}
@endforeach
...but instead of returning the full name, it is returning the whole JSON object like...
{"id":9,"email":"FunkB@example.com","fullname":"Brycen Funk","created_at":"2014-09-09 16:40:31","updated_at":"2014-09-09 16:40:31"}->fullname
Any thoughts on what I am doing wrong? I apologize in advance, because I am sure the answer is obvious and right before my eyes.
Thanks.
Dan
When your grabbing your Tasks from the DB using Eloquent, are you eager loading the User along with them? If not, to do this you'll just need to do something along these lines:
$tasks = Task::with(['user'])->get();
or alternatively you can do this within your foreach loop:
@foreach ($tasks as $task)
<tr>
<td>{{ $task->user()->fullname }}</td>
</tr>
@endforeach