Laravel 4: How to add scope to DB::table?

后端 未结 4 1994
北荒
北荒 2021-01-12 18:38

With Eloquent models adding scopes is easy:

public function scopeMyScope($query)
{
   // Do stuff to that $query
}

But how to add scope to

4条回答
  •  清歌不尽
    2021-01-12 19:12

    Been using lukas's solution works like a treat; I am calling the scope from the model where it has been defined it just avoids duplicating the function.

    $query = DB::table('page_views')
        ->where('id', $this->id)
        ->where('agent', 'NOT LIKE', '%bot%');
    $query = (new myModel())->scopeMyScope($query);
    $views = $query->count(DB::raw('distinct session, DATE(created_at)'));
    

提交回复
热议问题