Answer the question
In order to leave comments, you need to log in
Relationships in laravel, where is the error? Why does not see the table to which I am making a connection?
News table
class NewsModel extends Model
{
protected $primaryKey = 'id';
protected $table = 'news';
public function country()
{
return $this->hasOne(CountryModel::class);
}
}
class CountryModel extends Model
{
protected $primaryKey = 'id';
protected $table = 'country';
public $timestamps = false;
public function news()
{
return $this->belongsTo(NewsModel::class);
}
}
public function all()
{
$news = NewsModel::find(1);
return view('welcome', [ 'news' => $news]);
}
{{ dd($news->country->country) }}
Answer the question
In order to leave comments, you need to log in
1. It is worth checking the presence of the corresponding keys in the tables https://laravel.com/docs/5.3/eloquent-relationship...
2. See what error the parameter pulling from country gives
3. Redefine the keys manually
4. Test as indicated by the author of the answer above
It would be more logical to
use {{ dd($news->country-> name ) }}
Or do you have a country field in the country table?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question