Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4, ->withInput(); = Undefined offset: 0

Tags:

php

laravel

I have had a lengthy search both here and the Laravel forums, but i can't find an answer to this problem. ->withInput() coughs up an Undefined offset: 0.

For Context:

Controller

public function getJobs()

        {
            $position_options = DB::table('jposition')->lists('friendly','id');
            $category_options = DB::table('jcategory')->lists('friendly','id');
            $location_options = DB::table('jlocation')->lists('friendly','id');



            $result = $query->get();
            return View::make('jobsearch.search', array('position_options' => $position_options, 'category_options' => $category_options, 'location_options' => $location_options))->withInput();

        }

View

<form action="{{ action('JobsearchController@getJobs') }}" method="post">
  <div class="row">
    <div class="large-8 columns">
      <input type="text" name="realm" placeholder="Keywords/Skills" />
    </div>
    <div class="large-4 columns">
       {{ Form::select('category', $category_options , Input::old('category')) }}
    </div>
  </div>
  <div class="row">

    <div class="large-4 columns">
      {{ Form::select('location', $location_options , Input::old('location')) }}
    </div>


    <div class="large-4 columns">
      {{ Form::select('type', $position_options , Input::old('type')) }}
    </div>
    <div class="large-4 columns">
       <input type="submit" value="Search" style="width:100%; padding-top: .5rem;
padding-bottom: .5rem;" class="button border-btn" />
      </div>


</div>
</form>

Now according to the documentation there should not be an issue, and the page loads fine if the ->withInput(); is removed.

The end goal is to roll in the answer that i received from my previous question Undesired result from db:raw and have a single page that loads the "Filtering" form and displays the relevant results on the reload and remembers the selections in the form.

Thanks in advance.

UPDATE: Following a comment i have updated the controller and routes, still same result:

routes.php

Route::get('jobs/search', 'JobsearchController@getSearch');

&

Route::post('jobs/search', 'JobsearchController@getJobs');

Controller

 public function getSearch()
        {
                    $position_options = DB::table('jposition')->lists('friendly','id');
            $category_options = DB::table('jcategory')->lists('friendly','id');
            $location_options = DB::table('jlocation')->lists('friendly','id');

            return View::make('jobsearch.search', array('position_options' => $position_options, 'category_options' => $category_options, 'location_options' => $location_options));
        }

        public function getJobs()

        {
            $position_options = DB::table('jposition')->lists('friendly','id');
            $category_options = DB::table('jcategory')->lists('friendly','id');
            $location_options = DB::table('jlocation')->lists('friendly','id');


            return View::make('jobsearch.search', array('position_options' => $position_options, 'category_options' => $category_options, 'location_options' => $location_options))->withInput();

        }
like image 490
Rohan Peters Avatar asked Feb 25 '14 22:02

Rohan Peters


1 Answers

withInput() doesn't work the way you think it does. It's only a function of Redirect, not View.

Calling withInput($data) on View has a completely different effect; it passes the following key value pair to your view: 'input' => $data (you get an error because you're not passing any data to the function)

To get the effect that you want, call Input::flash() before making your view, instead of calling withInput(). This should allow you to use the Input::old() function in your view to access the data.

Alternatively, you could simply pass Input::all() to your view, and use the input[] array in your view:

View::make(...)->withInput(Input::all());

which is translated to

View::make(...)->with('input', Input::all());

As for your comment, I recommend doing it like so:

$position_options = DB::table('jposition')->lists('friendly','id');
$category_options = DB::table('jcategory')->lists('friendly','id');
$location_options = DB::table('jlocation')->lists('friendly','id');
$category = Input::get('category');
$location = Input::get('location');
$type = Input:: get('type'); 

$data = compact('position_options', 'category_options', 'location_options', 'category', 'type', 'location');

return View::make('jobsearch.search', $data);
like image 72
TonyArra Avatar answered Oct 30 '22 19:10

TonyArra