lundi 26 octobre 2015

Combine two request into one using laravel

I have two table email (email_id, email) and users (user_id) with a pivot table user_email (id, user_id, email_id) and i would like to check the existence of my email "toto@toto.com" for my user_id "2".

$emailID = Email::where('email', 'toto@toto.com')->pluck('id');
$exists = User::find('2')->emails->contains($emailID);

The result is ok, but i would like to know if it's possible to combine this into one query using Eloquent.

If have these models :

class Email extends Model {
    public function users() {
        return $this->belongsToMany('App\User', 'user_email');
    }
}

class User extends Model {
    public function emails() {
        return $this->belongsToMany('App\Email', 'user_email');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire