簡體   English   中英

Laravel 4下拉列表

[英]Laravel 4 drop down list

我創建了一個下拉列表,該列表從數據庫中獲取“ oneways”表上的“ destination-from”值。 在此遵循本教程: http : //www.laravel-tricks.com/tricks/easy-dropdowns-with-eloquents-lists-method

但是,每當我嘗試運行它時,都會出現此類錯誤: 未定義的變量:類別這里似乎是什么問題? 我真的很陌生。 laravel上的新手。

這是我的代碼:

onewayflight.blade.php

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

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

我不確定我遺漏了什么。 我也想知道模型是否與此有關?

模板中用於獲取類別的代碼必須用PHP標記包圍,否則將不會執行。 但更好的是-將其移動到它所屬的控制器中。

同樣在您的控制器中,您將視圖返回到onewayflight()的頂部-之后將不執行任何操作。

因此,像這樣更改它的工作方式,就可以了:

onewayflight.blade.php

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

onewayflightcontroller.php

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

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

另外,在routes.php中,路由應如下所示:

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

除了@ jd182的建議外(由於我缺乏評論的聲譽,因此我使用了答案); 您確定list()函數/ facade返回的值不是null PHP有時會將null值視為未定義。

而且刀片的語法錯誤。 如果要在刀片文件中定義變量(不建議使用),則應將其包裝在標記周圍,並將其作為常規php文件使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM