get the request values in Laravel
Asked Answered
F

3

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]); 
}
Fortis answered 2/11, 2017 at 14:56 Comment(3)
Could you edit your post showing what returns a dd($request->all()); in your controller?Wilbur
@Asur, hi dd result was, array:3 [▼ "datepicker_from" => "10/01/2017" "datepicker_to" => "10/31/2017" "customer_name" => "asdfasdf" ]Fortis
@Asur, I think the core is how to adjust 2 parameters to return view.Fortis
S
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

Sheers answered 2/11, 2017 at 15:27 Comment(0)
L
3

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

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

Lampion answered 2/11, 2017 at 15:15 Comment(0)
E
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.

Erlond answered 2/11, 2017 at 16:29 Comment(1)
$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.Sanjiv

© 2022 - 2024 — McMap. All rights reserved.