HTML Code
JavaScript
x
@foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">{{ $post->owner_user->username }}</p>
</div>
</div>
@endforeach
Laravel code
JavaScript
public function testpage()
{
return Posts::with(['OwnerUser','like'])->get();
}
when I just return Posts table, it has column owner_user with user information in it. relations work, but somehow I get “Null” as a result in {{ $post->owner_user->username }} when I return it in HTML code.
Advertisement
Answer
because that is not a single object try
JavaScript
@foreach($posts as $post)
<div class="container card mt-2" style="width: 32rem;">
<img src="{{ asset('images')."/".$post->img_url }}" class="card-img-top" alt="img not found">
<div class="card-body">
<h5 class="card-title">{{ $post->champion }} </h5>
<p class="card-text">@(foreach $post->owner_user as $user){{$user->username }}@endforeach</p>
</div>
</div>
@endforeach