0

When I try ti use global scope in my model:

    protected static function boot()
{
    parent::boot();
    static::addGlobalScope(new PostScope(Post::TYPE_VIDEO));
    static::addGlobalScope('video', function (Builder $builder) {
        $builder->join('video_posts', 'post_id', '=', 'id');
    });
}

It doesn't work, because somewhere Laravel process double-call function join(). Anybody encountered with it? How fix it?

  • you should try to avoid using the global scope, it will make maintaining the code harder later on. – online Thomas Sep 25 '17 at 08:49
  • You sure you have implemented as per [documentation](https://laravel.com/docs/5.4/eloquent#global-scopes) – Rahul Sep 25 '17 at 08:54
  • @ThomasMoors i have two class: post and videopost. Videpost have all fields that post and one additional. Create new class for one field - stupidly. – gidiberat Sep 25 '17 at 08:57
  • @RahulMeshram when I redefine join: public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false, $name = null) { if ($this->originalQuery->getQuery()->joins) { return $this->originalQuery; } return $this->originalQuery->join($table, $first, $operator, $second, $type, $where); } - all works good. – gidiberat Sep 25 '17 at 08:58
  • there is `inheritance` (extending the class) and `traits` that would make the code cleaner – online Thomas Sep 25 '17 at 08:58
  • @ThomasMoors I have class basepost and two extended classes Post and Videpost. And I use global scope to get videpost with fields which contains in post model. – gidiberat Sep 25 '17 at 09:00

1 Answers1

0

If you have a class 'video' I would recommend to add an Eloquent relation between your two classes using hasMany / belongsTo and just grab it using

static::addGlobalScope('video', function ($builder) {
    $builder->with('video');
});