在Laravel中获取请求值

时间:2017-11-02 14:56:44

标签: laravel laravel-query-builder laravel-request laravel-response

我希望通过datepicker进行搜索查询并选择字段。 我怎么能从下面的视图文件中获取请求值到控制器? 我在哪里可以修改代码?感谢。

index.blade.php

<div class="form-group col-sm-6">
    {!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/pdfs/job_finished_search'))) !!} 
       {!! Form::input('text', 'datepicker_from', null, ['placeholder' => 'Fra', 'id' => 'datepicker_from']) !!}
       {!! Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) !!} 
       {!! Form::select('customer_name', $jobs->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!}
       {!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!} 
    {!! Form::close() !!}
</div>

Controller.php这样

public function job_finished_search(Request $request, Job $jobs)
{

    $jobs = Job::onlyTrashed()
            ->whereBetween('created_at', array(
              (Carbon::parse($request->input('datepicker_from'))->startOfDay()),
              (Carbon::parse($request->input('datepicker_to'))->endOfDay())))
            ->where('customer_name', 'like', '%'.$request->customer_name.'%')
            ->orderBy('deleted_at', 'desc')
            ->paginate(15);

       if (empty($jobs)){
           Flash::error('Search result not found');
    }

    return view('pdfs.index', ['jobs' => $jobs]); 
}

3 个答案:

答案 0 :(得分:1)

要获取请求值,您可以使用get方法,请尝试:

$customer = $request->get('customer_name','default_value');

答案 1 :(得分:1)

有多种获取请求数据的方法,例如获取datepicker_from值,您可以使用以下任何一种

$request->datepicker_from 
$request->input('datepicker_from')
$request->get('datepicker_from')

选择你最喜欢的那个

参考https://laravel.com/docs/5.5/requests

答案 2 :(得分:0)

要获取请求值,您可以设置$input= $request->all()之类的对象。然后,您可以使用作为数组的对象来获取特定字段,例如访问您的日期选择器,您可以编写$input['datepicker_from']。在代码中声明$input= $request->all()对象之前,需要放置$jobs