Laravel 5将变量从控制器传递到视图

Laravel 5将变量从控制器传递到视图

问题描述:

I'm new to php platforms and what I'm trying to do is to pass a variable from the controller to view for populating a dropdown list with entries from the database, but whatever i'm trying is not working. I get the following error:

Undefined variable: products_create

I can't understand what I'm doing wrong.

Controller

public function create()
{
    $products_create = categories::all(['id', 'category']);

    return View::make('products.create', compact('id', 'category'));
}   

View

{!! Form::label('category', 'Categorie') !!}
{!! Form::select('category', $products_create) !!}

我是php平台的新手,我想做的是从控制器传递一个变量 用数据库中的条目填充下拉列表的视图,但无论我正在尝试什么都不起作用。 我收到以下错误: p>

未定义变量:products_create p> blockquote>

我无法理解我的意思 做错了。 p>

控制器 strong> p>

  public function create()
 {
 $ products_create  = categories :: all(['id','category']); 
 
返回View :: make('products.create',compact('id','category')); 
} 
  代码>  PRE> 
 
 

查看强> p>

  {!!  Form :: label('category','Categorie')!!} 
 {!!  Form :: select('category',$ products_create)!!} 
  code>  pre> 
  div>

You pass variables to a view in the form of an associative array:

return View::make('products.create', ['products_create' => $products_create]);

compact is a function that helps you build such an array where all keys are the same as the variable name. However you have to pass the actual variable name to the function:

return View::make('products.create', compact('products_create'));