Php 如何在Laravel中使用搜索进行快速加载

Php 如何在Laravel中使用搜索进行快速加载,php,laravel,laravel-5,laravel-5.2,Php,Laravel,Laravel 5,Laravel 5.2,我正在努力 Model::search($_POST['search_text'])->with('user')->get() 但这不起作用,我得到了“不存在的方法”错误(只是Model::search($\u POST['search\u text'])->get()起作用,或者只是class::where('id',2)->with('user')->get() 如何在搜索结果上快速加载 谢谢如果您希望在请求时搜索,那么最好在控制器功能中创建 public static fun

我正在努力

Model::search($_POST['search_text'])->with('user')->get()
但这不起作用,我得到了“不存在的方法”错误(只是Model::search($\u POST['search\u text'])->get()起作用,或者只是class::where('id',2)->with('user')->get()

如何在搜索结果上快速加载


谢谢

如果您希望在请求时搜索,那么最好在控制器功能中创建

public static function search($request, $query){
    if($request->has('string')){
         $query->where('column', 'LIKE', '%'.$request->get('string').'%');
       }
    }

public function postIndex(Request $request){
    Model::where(function($query)use($request){
        $this->search($request, $query);
    })->with('user')->get()
}

我不知道搜索函数是如何工作的,但我认为它返回一个键或NULL,您需要一个模型来加载。你能把
Model::search($\u POST['search\u text'])->get()
作为链中的第一个方法,即
Model::with(…)->search(…)->get()
@RossWilson,它对undefined method\Database\Query\Builder::search()进行错误的r调用吗你能展示一下你是如何定义你的
search
方法的吗?