2017-07-01 6 views
0

Ich habe folgende drei Modelle:Laravel 5.2 - Eine zu vielen oder viele zu viele Beziehung?

class Job extends Model { 

    public function jobShortlists() 
    { 
     return $this->hasMany('App\JobShortlist'); 
    } 
} 

class Jobseeker extends Model { 

    public function jobShortlists() 
    { 
     return $this->hasMany('App\JobShortlist'); 
    } 
} 

class JobShortlist extends Model { 

    public function jobseeker() 
    { 
     return $this->belongsTo('App\Jobseeker'); 
    } 

    public function job() 
    { 
     return $this->belongsTo('App\Job'); 
    } 
} 

Und die Migrationen:

Schema::create('jobs', function(Blueprint $table) 
{ 
     $table->increments('id'); 
     $table->string('job_title', 100); 
     ... 
}); 

Schema::create('jobseekers', function(Blueprint $table) 
{ 
     $table->increments('id'); 
     $table->string('name', 100); 
     ... 
}); 

Schema::create('job_shortlists', function(Blueprint $table) 
{ 
     $table->increments('id'); 
     $table->integer('jobseeker_id')->unsigned(); 
     $table->integer('job_id')->unsigned()->unique(); 

     $table->unique(array('jobseeker_id', 'job_id')); 

     $table->foreign('jobseeker_id') 
      ->references('id') 
      ->on('jobseekers') 
      ->onDelete('cascade'); 

     $table->foreign('job_id') 
      ->references('id') 
      ->on('jobs') 
      ->onDelete('cascade'); 
}); 

A jobseeker mehrere Aufträge zu einer Liste hinzufügen. Was ist das für eine Beziehung zwischen den 3 Tabellen? Ist es eine Eins zu viele oder viele zu viele?

Welche sind die richtigen Beziehungen, die in allen drei Modellen definiert werden sollten?

Antwort

0

Sie haben so etwas wie dieses

Jobs 
    - Job ID 
    - Else 

JobsJobShortListsPivot 
    - Job ID 
    - Shortlist ID 
    - Else 

JobShortLists 
    - ShortList ID 
    - JobSeeker ID 
    - Else 

    JobSeekers 
    - JobSeeker ID 
    - Else 

Auf Arbeitssuchende Modell:

function shortlists(){ 
    return $this->hasMany("\App\JobShortLists"); 
} 

Auf JobShortLists Modell:

function jobs(){ 
    return $this->belongsToMany("\App\Jobs", "JobsJobShortLists", 
      "Shortlist ID", "Job ID"); 
} 

Ich habe so benutzten und es gelang ihm vor und meine erste Lernquelle war:

http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

Hoffe es hilft dir auch.