简体   繁体   中英

How to save the customize columns in many to many relationship in laravel?

I have two table which is jobseekers and jobpositions.They are many to many relationship, so in jobseekers model, I put the relationship like this.

public function My_jobpositions(){
  return $this->belongsToMany("App\Models\Jobposition",'jobposition_jobseeker','jobseeker_id','jobposition_id')->withPivot('stage', 'status','interview_time')
     ->withTimestamps();
}

Here is the jobposition_jobseeker table look like 在此处输入图片说明 and when I try to save in (jobposition_jobseeker) table in controller, its only save the id of the row from jobseeker table instead of jobseeker_id in jobposition_jobseeker table, but i have another columns beside id which is jobseeker_id in jobseeker table, I just want to store the jobseeker_id , don't want to store the id of the row from jobseekers table in jobposition_jobseeker table.

  $jobposition = Jobseeker::find(Input::get('jobseeker_id'));          
   $jobposition->My_jobpositions()->attach(Jobposition::find($value),['stage' => $stage[$index],'status'=>$status[$index],'interview_time'=>$interview_time[$index]]);

This is the jobseeker table look like. 在此处输入图片说明

Can anyone guide me please, anyhelp would be highly appreciated.

Did u try using hasMany

return $this->hasMany("App\Models\Jobposition",'jobposition_jobseeker','jobseeker_id','jobposition_id')->withPivot('stage', 'status','interview_time')
 ->withTimestamps();`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM