简体   繁体   English

Laravel 4下拉列表

[英]Laravel 4 drop down list

I've made a drop down list that takes 'destination-from' values on 'oneways' table from the database. 我创建了一个下拉列表,该列表从数据库中获取“ oneways”表上的“ destination-from”值。 Following this tutorial here: http://www.laravel-tricks.com/tricks/easy-dropdowns-with-eloquents-lists-method 在此遵循本教程: http : //www.laravel-tricks.com/tricks/easy-dropdowns-with-eloquents-lists-method

But whenever I try to run it, It gives me this kind of error: Undefined variable: categories What seems to be the problem here? 但是,每当我尝试运行它时,都会出现此类错误: 未定义的变量:类别这里似乎是什么问题? I am really new to this. 我真的很陌生。 A newbie on laravel. laravel上的新手。

Here are my codes: 这是我的代码:

onewayflight.blade.php onewayflight.blade.php

  $categories = Category::lists('destination-from', 'title');
      {{ Form::select('category', $categories) }}

onewayflightcontroller.php onewayflightcontroller.php

  public function onewayflightresults()
{
  return View::make('content.onewayflight');

  $list = DB::table('oneways');
  $listname = Oneways::lists('destination-from');

  $content = View::make('content.onewayflight', array('list'=>$list, 'listname'=>$listname));
}

I am not really sure of what I have left out one this. 我不确定我遗漏了什么。 And I am also wondering if the model has something to do with this? 我也想知道模型是否与此有关?

The code in your template where you are fetching the categories needs to be surrounded by PHP tags otherwise it won't be executed. 模板中用于获取类别的代码必须用PHP标记包围,否则将不会执行。 But better still - move it to your controller which is where it belongs. 但更好的是-将其移动到它所属的控制器中。

Also in your controller you return the view at the top of your onewayflight() - nothing after this will be executed. 同样在您的控制器中,您将视图返回到onewayflight()的顶部-之后将不执行任何操作。

So change it work work like this and you should be ok: 因此,像这样更改它的工作方式,就可以了:

onewayflight.blade.php onewayflight.blade.php

{{ Form::select('category', $categories) }}

onewayflightcontroller.php onewayflightcontroller.php

 public function onewayflight()
 {
    $categories = Category::lists('destination-from', 'title'); 

    return View::make('content.onewayflight', array('categories' => $categories));
  }

Also, in routes.php the route should look like this: 另外,在routes.php中,路由应如下所示:

Route::get('/your-route-path', [
    'as' => 'your_route_name',
    'uses' => 'onewayflightcontroller@onewayflight'
]);

Additionally to @jd182 's advice (as I lack the reputation to comment, I use answer); 除了@ jd182的建议外(由于我缺乏评论的声誉,因此我使用了答案); are you sure that lists() function/facade returns some value other than null . 您确定list()函数/ facade返回的值不是null PHP, sometimes, considers null values as undefined. PHP有时会将null值视为未定义。

And your blade syntax is wrong. 而且刀片的语法错误。 if you want to define a variable inside a blade file (which is not advised) you should wrap it around tag and work it as a regular php file. 如果要在刀片文件中定义变量(不建议使用),则应将其包装在标记周围,并将其作为常规php文件使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM