vendredi 4 décembre 2015

Laravel 5 updating pivot table

I'm really hoping someone can help me out here this is driving me crazy!

I have two tables Candidates and Qualifications joined with a Pivot table called Results .

So I have my models all set up like this:

Candidate Model:

public function qualification()
{
    return $this->belongsToMany('App\Qualification', 'Results', 'candidate_id', 'qualification_id')->withPivot('status','cert_number','credits_required','credits_achieved')->withTimestamps();
}

Qualification Model:

public function candidate()
{
   return $this->belongsToMany('App\Candidate', 'Result', 'qualification_id', 'candidate_id')->withPivot('status','cert_number','credits_required','credits_achieved')->withTimestamps();
}

Result Model:

public function candidate()
{
   return $this->belongsTo('App\Candidate')->withTimestamps();
}

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

Now what I want to do is run a function in the candidate controller that when run will update a field in the pivot table (results).

So I have tried this but am having no luck. The function seems to run but the field remains the same.

public function claim($id)
{
    $candidate_id = Candidate::find($id);

    $candidate_id->qualification()->updateExistingPivot($candidate_id, ['status'=> 'COMPLETE']);

}

Can anyone see where I am going wrong? Many thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire