不应静态调用非静态方法App \ Models \ Post :: projects()

时间:2018-03-16 17:19:41

标签: laravel laravel-5

我想在我的项目中使用查询范围,但是它给出了上述错误。可能我在某个地方犯了错误但它是什么?

在模型中

public function projects($query) {
     return $query->where('category_id',13);
}

路线

Route::get('/{slug}', function ($slug) {
  $proje = \App\Models\Post::whereSlug($slug)->firstOrFail();
  $related_projects = \App\Models\Post::projects()->where('id','!=',$proje->id)
            ->orderBy('id', 'DESC')->select('title', 'slug')->get();
  return view('frontend.projects.projects_single', compact('proje','related_projects'));
})->name('projects.single');

感谢您的帮助..

1 个答案:

答案 0 :(得分:0)

public function scopeProjects($query) {
     return $query->where('category_id',13);
}

现在你可以打电话了

Route::get('/{slug}', function ($slug) {
  $proje = \App\Models\Post::whereSlug($slug)->firstOrFail();
  $related_projects = \App\Models\Post::projects()->where('id','!=',$proje->id)
            ->orderBy('id', 'DESC')->select('title', 'slug')->get();
  return view('frontend.projects.projects_single', compact('proje','related_projects'));
})->name('projects.single');

它将按预期工作

您可以找到有关本地scope()

的更多信息