-1

How can get relation Beetwen 3 tables

enfants [id,nom,prenom]

responsables[id,nom,prenom]

relations[id,libelle] // père, mère, frère ...

responsable_enfants[id,enfant_id,responsable_id,relation_id]

Model Enfant.php

public function responsables(){
    return $this->belongsToMany(Responsable::class, 'responsable_enfants', 'enfant_id', 'responsable_id');
}

So can i get relation betwen enfants and responsable (père or mère ...)?

Mohamed JK
  • 11
  • 3
  • you completly dont need the table `relations`, replace the field relation_id with the libelle. you can add and enum to the field to limit the possible values (relation nature are not infinite) or even replace it with an integer and have the correspondance in code (constants) – N69S Oct 23 '20 at 15:05
  • @N69S Yes good idea but it's obligation to use the table relations – Mohamed JK Oct 23 '20 at 15:09
  • then take a loot and the class Pivot (it's used not completly like a model but you can declare it for the pivot table responsable_enfants and have relations on it) – N69S Oct 23 '20 at 15:31
  • `protected $fillable = [ 'enfant_id', 'responsable_id', 'relation_id' ]; public function relation(){ return $this->belongsTo(Relation::class, 'relation_id'); }` I create it but how can i access to the table relation ? – Mohamed JK Oct 23 '20 at 15:36
  • https://laravel.com/docs/8.x/eloquent-relationships#defining-custom-intermediate-table-models and https://laravel.com/api/5.8/Illuminate/Database/Eloquent/Relations/Pivot.html has relationships (since it extends model) – N69S Oct 23 '20 at 15:41
  • I use one method easy s starting by the table pivote and in the table pivote i make all relation between the 3 tables, so it easy to manipulate – Mohamed JK Oct 24 '20 at 08:51

1 Answers1

-1

I use one method easy s starting by the table pivote and in the table pivote i make all relation between the 3 tables, so it easy to manipulate

Mohamed JK
  • 11
  • 3