Two models I defined are having a 1to1 relationship, which I defined as:
Person:
public function contact(): HasOne
{
return $this->hasOne(Contactdata::class,'person_id', 'id');
}
Contactdata:
public function person(){
return $this->belongsTo(Person::class);
}
In a controller called "Userform" I have two methods, which query the person model.
The one where I query a single record
...
$person['contact'] = Person::find($id)->contact->toArray();
...
works flawlessly and returns the contactdata, while the one where I want to print a list doesn't work:
$result = Person::with(['details', 'contact'])->get();
The arraykey 'details' gets filled with DB result and works while 'contact' just returns null, even though the querylog clearly shows, that the query for contactdatas is correct and if manually executed return the correct resultset.
Am I missing something here?
I solved it after hours of working on it.
For some - odd - reason, when getting a single record it ignore the typo in the naming of person_id: Even though the column name was person_Id (with capital i), the result worked when querying a single record but not if multiple records are getting requested. So I just renamed person_Id to person_id and it finally worked.