I suggest giving this course a watch.
Trouble loading relationship
I'm trying to load a relationship in one of my controllers and it's not working. The thing is I have set up relationships before and they're all working correctly but for some reason this one is just not. Even copying directly from a relationship of the same type... hopefully someone else has an idea?
For context I am creating a virtual pet site.
I have a UserAdopt model for owned adopts. I have an Adopt model for adopts created through the admin area. The UserAdopt BelongsTo the Adopt model using the foreign key 'adopt'.
Here is the relationship in UserAdopt:
public function adopt(): BelongsTo {
return $this->belongsTo(Adopt::class, 'adopt');
}
This is how I am calling it in the controller to display on the adopt profile:
'adopt' => UserAdopt::with(['user', 'originalOwner', 'adopt'])->find($adopt->id),
The other 2 relationships work just fine.
I have tried both eager and lazy loading the 'adopt' relationship by itself and then dumping the data and it always fails and just dumps the ID number found in the adopt column, so it's not loading the model correctly.
In the migration, this is how I have set up the foreign ID:
$table->foreignIdFor(Adopt::class, 'adopt')->constrained()->cascadeOnDelete();
I know that it is connected properly on a database level because when I use mySQL I can click the ID number to go to the correct item in the adopts table.
I have also tried changing the name of the function from 'adopt' to something else just in case it was conflicting with something but no matter what I call it it still doesn't load correctly.
Does anyone have any ideas? Thank you...
Please or to participate in this conversation.