After several attempts, I can not make an eloquent "relations" work.
I created a database with MySQL Workbech which contains the following tables:
- Actors
- ActorData
- Nationality
Their relationship image:
In PHP instead I have the following code (summary):
class ActorData extends Model
{
protected $table = 'actordata';
[..]
public function nationality()
{
return $this->hasOne("App\Models\ActorData\Nationality");
}
}
class Actor extends Model
{
protected $table = 'actors';
[..]
public function actorData()
{
return $this->hasOne('App\Models\ActorData');
}
}
My goal is, when i call "->nationality", i wish it takes the "nationality", but i recive "null" or errors about "keys" not found.
$actorData = \App\Models\Actor::find(1)->actorData()->first();
var_dump($actorData->nationality);
I understand that "hasOne" looks for the key in "nationality", but I wanted to make a simple "link". As another solution, so I could simply take the "Nationality_Id" and look for it in the "Nationality" table, but I think it's a wrong approach.
I hope I have been exhaustive, thanks for your cooperation.
( in order to "fix" i had to add "$actorData->nationality()->associate($nationality))
You relationship should look something like the above.
Where actorData is a pivot table for the 2 other tables.
Your classes should look like this:
You're basically looking at Many-to-Many. Where each actor can have many nationalities while at the same time each nationality can belong to many actors.
Normally laravel expects the pivot table to be named something like
actor_nationality
, but you can specify the table name: