【问题标题】:How to get data from database displaying in a list laravel如何从列表中显示的数据库中获取数据 laravel
【发布时间】:2015-07-12 18:59:11
【问题描述】:

每当我使用此代码时,我都会收到以下错误: "试图获取非对象的属性"

控制器:

public function createBooking()
{
    $products = DB::table('products')->orderBy('id', 'asc')->lists('id', 'name');
    return View::make('users.addbooking', array('products' => $products));
}

刀片视图:

{{ Form::select('products', $products , Input::all('products')) }}

【问题讨论】:

  • $products 的输出是什么?你试过在->lists('id', 'name') 之后添加->get() 吗?
  • 您添加了use DB; 吗?
  • 您完全确定这些代码段出错了吗?
  • 我感觉是因为Form::select方法中的第三个参数。第三个参数应该是您的默认值(如果您想要一个),Input::all('products') 作为默认值对我来说似乎不正确,因为您在不期望的情况下将整个数组扔到默认值。也许尝试将其设置为null,因为它在页面加载时为空,如果页面重新加载,它将自动添加Input::old('products')。在此处查看文档:laravel.com/docs/4.2/html#drop-down-lists

标签: php arrays database list laravel


【解决方案1】:

试试这个,我正在运行 laravel 5,它对我来说很好。

控制器:

public function createBooking()
{
    $products = DB::table('products')
        ->orderBy('id', 'asc')
        ->lists('name', 'id'); // checked some of my code I have these swapped value first then id
    return View::make('users.addbooking', array('products' => $products));
}

刀片:

{{ Form::select('products', ['' => 'Select'] + $products, '', ['class' => 'myClass', 'id' => 'myId'] }}

如果您收到 Trying to get property of non-object 错误,请在控制器中查询后尝试此操作,以检查您是否获得了正确的查询结果:

dd($products);

【讨论】:

  • 我升级到了 Laravel 5,这很有效。不知道错误是什么。谢谢!
  • 很高兴你最终让它工作了。很高兴能帮到你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
相关资源
最近更新 更多