【问题标题】:How to show where parent_id = value in laravel如何在laravel中显示parent_id = value的位置
【发布时间】:2019-05-22 07:08:17
【问题描述】:

我有一个类别表,它们有以下字段。

  • 身份证
  • 姓名
  • parent_id

我想显示 parent_id =(例如)= 2 的类别,但我看到了这个错误。

错误异常 (E_ERROR)

试图获取非对象的属性(查看:C:\xampp\htdocs\new\shopping\resources\views\Home\networks.blade.php) 以前的例外情况

试图获取非对象的属性(0)

NetworkController.php

public function networks()
{
    $categories = Category::where('parent_id', 2)->first();
    return view('Home.networks', compact('categories'));
}

networks.blade.php

@foreach($categories as $category)
    <option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach

【问题讨论】:

    标签: laravel-5.8


    【解决方案1】:

    $categories = Category::where('parent_id', 2)->first();

    只需将其替换为

    $categories = Category::where('parent_id', 2)->get();
    

    作为参考,请查看此链接 the difference of find and get in Eloquent in Laravel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2019-11-04
      • 2012-12-07
      • 2023-04-05
      • 2017-03-30
      • 2018-05-03
      • 2020-02-25
      相关资源
      最近更新 更多