Reference: Laravel Eloquent relationship not an object when doing anything but dd()
I am trying to output a Laravel relationship in Blade. However, this, {{ $video->channel->id }} returns a non-object error. But, when dded, like so, {{ dd($video->channel->id) }}, a value is there. I've been pulling my hair so hard because of this... What is going on? Why is there an output only when the variable is dded?
I'm testing on PHP 7.2 with Laravel 5.6. Relationships are established as per Eloquent documentation. I've tried fetching the data like this:
$videos = Video::where('foo', 'bar')->with('channel')->take(100)->get();
and
$videos = Video::where('foo', 'bar')->take(100)->get();
The same thing; same output/error.
$tokens = preg_split("/[\s,]+/", $q);
$videos = Video::with('channel')
->where(function ($query) use ($tokens) {
foreach ($tokens as $token) {
$query->orWhere('title', 'LIKE', "%$token%");
}
})
->take(100)
->get();
// foreach ($videos as $video)
{{ $video->channel->id }} // non-object error
{{ dd($video->channel->id) }} // WORKS! IDK Why...
Trying to get property 'id' of non-object is the actual error log.
EDIT:
Below is an image of the $videos collection fetched from Database.
If in dd(), something gets outputted fine, I can only assume I'm fetching the data right, yea?
