mardi 20 septembre 2016

Laravel - updating a field on a relationnal table

I have a users table, a emails table and a users_emails table. In that last one, I have the ids of the other two, and another field : is_confirm. When a user confirms his email, I want to update that field, and tries to do so using the models.

In the User model :

public function emails() {
    return $this->belongsToMany('App\Email', 'user_email')->withPivot('is_confirm');
}

In the Email model :

public function users() {
    return $this->belongsToMany('App\User', 'user_email')->select(['user_email.is_confirm', 'user_email.id']);
}

I tried things like

$emailID = Email::where('email', $email)->first();
$pivot = Email::find($emailID->id)->users->first();
$pivot->is_confirm = 1;
$pivot->save();

or

$emailID = Email::where('email', $email)->first();
$emailID->users()->updateExistingPivot( $emailID->user_id, ['is_confirm' => 1] );

Doesn't work.

So, are my models ok ? And what should I do to update this field ?

Thanks ahead !



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire