duanliao2310
2015-04-12 12:55
采纳率: 100%
浏览 44
已采纳

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

图片转代码服务由CSDN问答提供 功能建议

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

未定义变量:products_create

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

控制器

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

查看

 <代码> {!!  Form :: label('category','Categorie')!!} 
 {!!  Form :: select('category',$ products_create)!!} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • doumengbai2031 2015-04-12 13:00
    最佳回答

    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'));
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题