【问题标题】:Laravel 4, ->withInput(); = Undefined offset: 0Laravel 4,->withInput(); = 未定义的偏移量:0
【发布时间】:2014-04-25 22:39:22
【问题描述】:

我在这里和 Laravel 论坛都进行了长时间的搜索,但我找不到这个问题的答案。 ->withInput() 咳出Undefined offset: 0

对于上下文:

控制器

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

        }

查看

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

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

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

提前致谢。

更新: 在发表评论后,我更新了控制器和路由,结果仍然相同:

routes.php

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

&

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

控制器

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

        }

【问题讨论】:

  • 我自己是 laravel 新手,但是当你第一次加载视图时,你不会有任何 Inputs 吗?

标签: php laravel


【解决方案1】:

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

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

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

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

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

翻译成

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

至于你的评论,我建议这样做:

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

【讨论】:

  • TonyArra 您的回答帮助解决了这个问题,只是想澄清一下。我可以在-&gt;withInput(Input::all()); 之后添加-&gt;with('result', $result); 而不会产生不利影响吗?还是需要以某种方式重组?
  • @RohanPeters 我相信你可以,但我建议你构建一个包含你需要的所有东西的 $data 数组,这样你就可以像这样调用你的视图return View::make(jobsearch.search, $data)
猜你喜欢
  • 2014-06-25
  • 2015-12-20
  • 2020-01-02
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 2019-03-21
  • 2018-10-09
相关资源
最近更新 更多