【问题标题】:Laravel 4 error: Undefined variable [duplicate]Laravel 4错误:未定义的变量[重复]
【发布时间】:2014-11-28 10:35:53
【问题描述】:

我做了一个列表查询来填充我的下拉框。我在控制器部分尝试了 var_dump,一切顺利,但是每当我尝试在刀片模板上调用我的函数时,它都会返回一个错误:未定义的变量:类别(查看:C:\wamp\www\airlines\ app\views\content\onewayflight.blade.php)

这里似乎有什么问题?

OnewayflightController.php

   public function onewayflight()
{ 
  $categories = DB::table('oneways')->lists('destination-from');
  return View::make('content.onewayflight')->with('destination-from', $categories);
}

onewayflight.blade.php

{{ Form::select('destination-from', $categories) }}

routes.php

Route::get('flight/onewayflight','OnewayflightController@onewayflight');

【问题讨论】:

标签: php laravel laravel-4 blade


【解决方案1】:

你应该在 Blade 中使用:

{{ Form::select('destination-from', $destination-from) }}

因为在您使用的方法中:

with('destination-from', $categories)

所以你说在 Blade 中 $categories 必须命名为 $destination-from

但是你不能在变量名中使用-,所以你应该把它改成:

with('destinationFrom', $categories)

在 Blade 中:

{{ Form::select('destination-from', $destinationFrom) }}

【讨论】:

  • 我敢打赌 $destination-from 不是一个有效的变量名。 ${'destination-from'} 可以工作。
  • @Jari 我知道,我在回答中添加了额外的解释
  • @MarcinNabiałek 谢谢先生!现在,这对我来说很清楚。先生,还有一个问题,如果我要填充两个单独的下拉菜单怎么办?如何正确实施?
  • @staphhelpme 如果您使用相同的数据,您只需使用相同的数据创建新的选择(当然还有其他选择名称)。如果需要更多数据,需要在with方法中传递数组
  • @staphhelpme 你需要使用->with(['destination-from'=> $categories, 'other-variable' => $user]);等等
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 2014-10-09
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
相关资源
最近更新 更多