mardi 18 décembre 2018

Laravel: wherePivot or where clause on collection on many to many relation table not working?

I have a many to many relationship between roles and permissions tables. I am trying to get all the roles and permission where pivot.is_deleted = 0. But the where clause doesn't seem to work. What am I doing wrong? I also tried this: On My roles model:

public function permissions()
    {
        return $this->belongsToMany(Permission::class,'permission_role','role_id','permission_id')->wherePivot('is_deleted', '=', 0);
    }

I have done this similarly on my permissions model This is what I have tried in my controller:

$roles = Role::all();
dd($roles); //gives all data without filtering.

$roles= Role::where('is_deleted',0)->whereHas('permissions', function($query){
    $query->where('permissions.pivot.is_deleted', 0);
})->get();

.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire