Sinnbeck's avatar
Level 102

Scopes in join

Does anyone know of any way to allow the usage of scopes in join clauses? Perhaps a package.

Using it just throws and error.

Call to undefined method Illuminate\Database\Query\JoinClause::active()
0 likes
8 replies
Sinnbeck's avatar
Level 102

@vandan Thanks. I know of the api. The issue is that I have this scope

public function scopeActive($query)
    {
        $query->where($this->getTable() . '.type', '>', 1);
    }

That I wish to use like this (sample code for demonstration only)

Project::join('groups', function($join) {
    $join->on('groups.project_id', 'project.id')->active(); //the active scope function does not work
})->get();
1 like
click's avatar
click
Best Answer
Level 35

Not really what you are looking for probably but can't you "solve" this with a work around to combine your join + where clause into a separate scope? Like this:

public function scopeJoinActiveGroups($query) 
{
      $query->join(/* */);
      $query->where($this->getTable() . '.type', '>', 1);
}
Project::joinActiveGroups()->get();
Sinnbeck's avatar
Level 102

That is actually not a bad idea! :) I will look into implementing that as a quick test :)

pimski's avatar

@sinnbeck did you solve your problem with the proposed solution? Or did you find a better solution?

I'm also looking for a clean way to use the logic from a scope in a query join...

Rikaelus's avatar

Has anyone found a way to do this? I'm working with a system with pretty advanced logic behind scopes and I really need to be able to use them against joined tables.

Please or to participate in this conversation.