【发布时间】:2016-02-18 21:28:49
【问题描述】:
我正在尝试在 Laravel 5.2 的下拉列表中显示数组中的数据。这是我的数组的样子:
这是另一个关于来自我的控制器的return 数组数据的演示。
这是我的视图的样子,它是我尝试创建的简单下拉列表:
<select class="form-control" name="service_id">
<option value="">Select...</option>
@foreach($serviceslist as $serviceli)
<option value="{{$serviceli->id}}">
{{$serviceli->servicename}}
</option>
@endforeach
</select>
而我在 laravel 中的 controller 函数是这样的:
public function create()
{
$services = $this->getCategories();
return View::make('services.create')->with('serviceslist', $services);
}
private function getCategories($parentId = 0)
{
$categories = [];
foreach(Services::where('service_id', $parentId)->get() as $category)
{
$categories[] = [
'item' => $category,
'children' => $this->getCategories($category->id)
];
}
return $categories;
}
这里我在同一个控制器中创建了一个函数getCategory() 来创建数组。
当我运行代码时,我得到了这个错误。
Trying to get property of non-object (View: /Applications/MAMP/htdocs/laravelCRM/resources/views/services/create.blade.php)
我也尝试在我的视图(刀片模板)上执行getCollection(),但它也没有工作。问题出在哪里?
谢谢! (提前)
【问题讨论】: