Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

php - Laravel Eloquent relationship (5 tables)

I have 5 tables in a database

  1. Volume: id, volume, number,...
  2. Papers: id,volume_id,title,...
  3. Editor_name: id,idP(Papers ID),name...
  4. Email: id, idA(Editor_name_id), email
  5. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get all information like this way :

public function show($paper_id) {

     $data = Papers::with('volume', 'names', 'names.email', 'names.institution')->findOrFail($paper_id);

     dd($data);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...