dimanche 28 août 2016

Query with pivot table laravel 5.1

Query with pivot table laravel

Mappings

enter image description here

I am having a trouble with pivot query.

App/Entities/user.php

public function roles()
{
    return $this->belongsToMany('App\Entities\UserRole', 'user_user_roles', 'user_id', 'user_role_id');
}


public function getPivotCondition($roleId)
{
    return $this->roles()->wherePivot('user_role_id',$roleId);
}

Controller

    public function getUserWithRole(){

    $users = $User->getPivotCondition(1)->get();

    dd($users);

    }        

It results in a query

    SELECT `user_roles`.*, 
       `user_user_roles`.`user_id`      AS `pivot_user_id`, 
       `user_user_roles`.`user_role_id` AS `pivot_user_role_id` 
FROM   `user_roles` 
       INNER JOIN `user_user_roles` 
               ON `user_roles`.`id` = `user_user_roles`.`user_role_id` 
WHERE  `user_user_roles`.`user_id` IS NULL 
       AND `user_user_roles`.`user_role_id` = '1' 

my question is that why i am getting user_user_roles.user_id is null in my query

I know that i can use WhereHas but i am looking for an alternative.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire