1
I’m doing some exercises, but there is one but I can’t understand why it’s not working when I do it with another name in the method. I have a method in the Model Location
, where he does the relationships
of OneToOne
.
When I call the method with the simple name it works, more when I call the method with the compound name it from null
.
Model Location
public function countryInverso()
{
return $this->belongsTo(Country::class);
}
Controller Onetoone
public function umPraUmReverso()
{
$location = Location::where('latitude', 54)->get()->first();
$country = $location->countryInverso;
dd($country);
}
Thus returns from dd()
null
Now if I put it that way
Model Location
public function country()
{
return $this->belongsTo(Country::class);
}
Controller Onetoone
public function umPraUmReverso()
{
$location = Location::where('latitude', 54)->get()->first();
$country = $location->country;
dd($country);
}
This last way it comes back to me the data, because, with the name in the method countryInverso
does not work and only with the name country
works?
as is the name of the foreign key ?
– DNick
country_id and my foreign key name
– Natan Melo