Answer the question
In order to leave comments, you need to log in
Links not working properly?
I'm trying to display related tables, news and countries
$news = NewsModel::with('country')->paginate(1);
return view('welcome', ['news' => $news]);
public function country()
{
return $this->belongsTo(CountryModel::class, 'country', 'id');
}
@foreach($news as $item)
{{ $item->country->country }}
@endforeach
Answer the question
In order to leave comments, you need to log in
holfza gave the right advice to rename the method in the model different from the name of the cell in the database.
If one $item element does not have a country, then of course it will fail.
Check before output:{{ $item->country ? $item->country->country : ''}}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question