Laravel 4, ->withInput(); = 未定义的偏移量:0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22027830/
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 09:06:03  来源:igfitidea点击:

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

phplaravel

提问by Rohan Peters

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.

我在这里和 Laravel 论坛都进行了长时间的搜索,但我找不到这个问题的答案。->withInput()咳了一声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.

现在根据文档,应该没有问题,如果->withInput();删除了页面,则页面加载正常。

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

最终目标是将我从我之前的问题db:raw 的 Undesired result 中收到的答案滚动,并有一个页面加载“过滤”表单并在重新加载时显示相关结果并记住表单中的选择。

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::get('jobs/search', 'JobsearchController@getSearch');

&

&

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

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

        }

回答by TonyArra

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

withInput()不像你认为的那样工作。它只是重定向的功能,而不是视图。

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)

调用withInput($data)View有完全不同的效果;它将以下键值对传递给您的视图:('input' => $data您收到错误,因为您没有将任何数据传递给函数)

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.

要获得您想要的效果,请Input::flash()在创建视图之前调用,而不是调用withInput(). 这应该允许您Input::old()在视图中使用该函数来访问数据。

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

或者,您可以简单地传递Input::all()给您的视图,并input[]在您的视图中使用该数组:

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