【问题标题】:Create nested dropdown style in Laravel array在 Laravel 数组中创建嵌套下拉样式
【发布时间】: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(),但它也没有工作。问题出在哪里?

谢谢! (提前)

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    您将数组而不是 Collection 的实例传递给视图,因此您不能在数组上运行 getCollection()

    return $categories; 替换为return collect($categories);

    你也可以return Collection::make($categories);

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 2020-01-28
      • 1970-01-01
      • 2021-08-24
      • 2021-08-30
      • 2013-01-14
      • 1970-01-01
      • 2020-05-03
      • 2015-07-12
      相关资源
      最近更新 更多