I am building an immigrant control application and own an imigrants table that has 3 auto relationships. Its structure is as follows.
- id - > pk
- first_name - > varchar
- last_name - > varchar
- (...)
- father - > integer (fk)
- mother - > integer (fk)
- spouse - > integer (fk)
But I can not determine the right relationship with eloquent (I'm still learning to use laravel). I have already tested with hasOne, belongsTo etc. and I can not make it associate properly. When using for example the query:
$imigrant->father()->save($father)
Added to the $ imigrant id in $ father. Contrary to what I desire.
And if you use belongsTo()
with associate()
, eg:
$imigrant->father()->associate($father)
He arrow null in the $ imigrant id and makes a merge of $ father getting type:
App\Imigrant {#680
id: null,
first_name: "FName",
last_name: "LName",
birth_place: null,
birth_date: null,
disembark_place: null,
disembark_date: null,
father: App\Imigrant {#676
id: 1,
first_name: "FName2",
last_name: "LName2",
birth_place: null,
birth_date: null,
disembark_place: null,
disembark_date: null,
father: null,
mother: null,
spouse: null,
ship: null,
created_at: "2016-12-26 14:59:17",
updated_at: "2016-12-26 14:59:17",
},
mother: null,
spouse: null,
ship: null,
created_at: "2016-12-26 15:12:16",
updated_at: "2016-12-26 15:12:16",
And it does not update the database.
I know I could do the manual association, but I wanted to use the eager query features that associate the data and automatically pull the associations in the search. If someone can give me a light on how I could do the association in the Model to make it work. Otherwise, I'll have to make do with my nails. : D