Rohan Peters
Rohan Peters

Reputation: 147

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

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();

        }

Upvotes: 11

Views: 15109

Answers (1)

Tony Arra
Tony Arra

Reputation: 11119

withInput() is only a method on the Redirect class. It is not available on the View class.

Calling View.withInput($data) has different behavior: it passes the following key value pair to your view: 'input' => $data. This causes an error in your code because you're not passing any data to the function.

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

Alternatively, you could 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());

Upvotes: 32

Related Questions