简体   繁体   中英

ManyToMany Eloquent Laravel query issue with whereHas

I have a ManyToMany relationship between two models: Album and Media .

Here you can see Album model:

class Album extends Model
{
    public function medias()
    {
        return $this->belongsToMany('\Api\Medias\Models\Media');
    }
}

And here Media model:

class Media extends Model
{
    public function albums()
    {
        return $this->belongsToMany('\Api\Medias\Models\Album');
    }
}

Now I want to select all medias that belongs to albums with public=1 . The eloquent query is this:

return $this->getModel()::whereHas('albums', function($query) {
                $query->where('public', '=', '1');
            })->get();

But I get this error in whereHas line:

local.ERROR: ErrorException: compact(): Undefined variable: operator in...

I am using Laravel Framework 5.4.36 .

It is like a 5.4 issue. ( https://github.com/laravel/framework/issues/26936 ) You should try to upgrade from 5.4 to 5.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM