dtlrp119999 2015-12-09 01:08
浏览 69
已采纳

将Eloquent \ Builder作为参数传递

Probably it's an easy answer, but I don't find it! I'm using Eloquent to perform my queries into the database, having this code:

public function postSearch(Request $request)
{
    $query = Customer::select('identification', 'first_name', 'second_name', 'first_lastname', 'second_lastname', 'genre')
                ->where('identification', 'LIKE', "%$request->identification%")
                ->where('first_name', 'LIKE', "%$request->first_name%")
                ->Where('second_name', 'LIKE', "%$request->second_name%")
                ->Where('first_lastname', 'LIKE', "%$request->first_lastname%")
                ->Where('second_lastname', 'LIKE', "%$request->second_lastname%")
                ->Where('genre', 'LIKE', "%$request->genre%");
    $request->session()->put('list', $query->get());
    $customers = $query->paginate(20);
    return view('customer.index', compact('customers'));
}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index(Request $request)
{
    $query = Customer::select('identification', 'first_name', 'second_name', 'first_lastname', 'second_lastname', 'genre');
    $request->session()->put('list', $query->get());
    $customers = $query->paginate(20);
    return view('customer.index', compact('customers'));
}

It works perfectly, but now, I want to optimize it creating another funtion which receives the $query. Something like this:

public function displayResult(Builder $query,Request $request)
    {
        $request->session()->put('list', $query->get());
        $customers = $query->paginate(20);
        return view('customer.index', compact('customers'));
    }

The objective is to call the function from my previous methods, e.g:

public function index(Request $request)
    {
        $query = Customer::select('identification', 'first_name', 'second_name', 'first_lastname', 'second_lastname', 'genre');
        $this->displayResult($query, $request);
    }

But, my view is not displaying nothing. Am I doing something wrong? Should I use a Model instead Builder parameter? If I do so, then it's displaying this error:

Argument 1 passed to App\Http\Controllers\CustomerController::displayResult() must be an instance of App\Http\Controllers\Model, instance of Illuminate\Database\Eloquent\Builder given, called in ....on line 54 and defined

That line is exactly where I call to my new funtion. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doucandiao9180 2015-12-09 20:41
    关注

    Your displayResult() method returns the view instance, but you forgot to return it from your controller's index() method.

    Replace

    $this->displayResult($query, $request);
    

    with

    return $this->displayResult($query, $request);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法