在 Laravel 中获取请求值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47078330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:54:23  来源:igfitidea点击:

get the request values in Laravel

laravellaravel-query-builderlaravel-requestlaravel-response

提问by JsWizard

I wish to make search query by datepicker and select field. How could I get the requests values from below view file to controller? Where could I modify in the code? thanks.

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

index.blade.php

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

控制器.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]); 
}

回答by rchatburn

There are multiple way to get the request data e.g to get datepicker_from value you can use any of the below

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

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

choose the one you like the most

选择你最喜欢的

refer to https://laravel.com/docs/5.5/requests

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

回答by ka_lin

To get request values you can use the getmethod, try:

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

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

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

回答by Olasunkanmi

To get request values, you can set an object like $input= $request->all(). Then you can make use of the object which is an array to get at specific fields, e.g to access your date picker, you can write $input['datepicker_from']. You need to place $input= $request->all()before you declare the $jobsobject in your code.

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