lundi 3 avril 2017

Using a global scope on laravel 5.4 with data from pivot table

I am trying to create a global scope for my model, Project, in Laravel 5.4.

I have a project_user many to many relationship and I can query data from the pivot table in all other parts of the project. I am just having trouble setting a global scope that uses data from the pivot table.

I would like to be able to set the scope to the pivot column "dropped_out", which is a boolean identifying whether or not a user has dropped out of the project.

Here is the scope in Project.php:

protected static function boot()
{
    parent::boot();
    static::addGlobalScope('active_users', function (Builder $builder) 
    {
        $builder->where('pivot.dropped_out', '=', 0);
    });
}

This returns "Unknown Column 'pivot.dropped_out'". I have tested this scope with a column straight from the Projects table ('id', '>', 50) and it works fine. Just don't know how to access the project_user -> dropped_out column.

What would be the best way to access the pivot table here?

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire