I have 5 tables in a database
- Volume: id, volume, number,...
- Papers: id,volume_id,title,...
- Editor_name: id,idP(Papers ID),name...
- Email: id, idA(Editor_name_id), email
- Institution: id, idA(Editor_name_id), institution
And I would need to display all info about article (data from all tables) in one blade.
Example
URI : papers/{paper_id}
Volume 1 Number 1
Title 1
Editor_name1, email 1, email 2, institution 1
Editor_name2, email 3, institution 2, institution 3
...
Migrations:
Volume:
Schema::create('volume', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer("volume");
$table->integer("number");
$table->timestamps();
});
Papers:
Schema::create('papers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("title");
$table->integer("volume_id");
$table->foreign('volume_id')->references('id')->on('volume')->onDelete('cascade');
$table->timestamps();
Editor_name:
Schema::create('editor_name', function (Blueprint $table) {
$table->id();
$table->integer("idP");
$table->string("name");
$table->timestamps();
$table->foreign('idP')->references('id')->on('papers')->onDelete('cascade');
});
Email:
Schema::create('email', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer("idA");
$table->string("email");
$table->timestamps();
$table->foreign('idA')->references('id')->on('editor_name')->onDelete('cascade');;
});
Institution:
Schema::create('institution', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer("idA");
$table->string("institution");
$table->timestamps();
$table->foreign('idA')->references('id')->on('editor_name')->onDelete('cascade');
});
Models:
Volume:
public function names()
{
return $this->hasManyThrough(Names::class, Papers::class,'issue','idP','id','id',);
}
public function papers()
{
return $this->hasMany(Papers::class,'issue');
}
Papers:
public function volume()
{
return $this->belongsTo(Volume::class,'issue');
}
public function names()
{
return $this->hasMany(EditorName::class,'idP');
}
Names:
public function papers()
{
return $this->belongsTo(Papers::class,'idP');
}
public function email()
{
return $this->hasMany(Email::class,'idA');
}
public function institution()
{
return $this->hasMany(Institution::class,'idA');
}
Email:
public function names()
{
return $this->belongsTo(Names::class,'idA');
}
Institution:
public function names()
{
return $this->belongsTo(Names::class,'idA');
}
question from:
https://stackoverflow.com/questions/66051082/laravel-eloquent-relationship-5-tables