6

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

<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]); 
}
JsWizard
  • 1,663
  • 3
  • 21
  • 48
  • Could you edit your post showing what returns a `dd($request->all());` in your controller? – Asur Nov 02 '17 at 15:03
  • @Asur, hi dd result was, array:3 [▼ "datepicker_from" => "10/01/2017" "datepicker_to" => "10/31/2017" "customer_name" => "asdfasdf" ] – JsWizard Nov 02 '17 at 15:07
  • @Asur, I think the core is how to adjust 2 parameters to return view. – JsWizard Nov 02 '17 at 15:13

3 Answers3

8

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

$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

rchatburn
  • 732
  • 4
  • 13
3

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

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

ka_lin
  • 9,329
  • 6
  • 35
  • 56
0

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 $jobs object in your code.

Nissa
  • 4,636
  • 8
  • 29
  • 37
Olasunkanmi
  • 323
  • 6
  • 18
  • `$input= $request->all()` returns an array, not an object. The explained method is good to know for people but you would better fixing your wording to be precise. – Valentine Shi Mar 21 '20 at 07:33