Facade\Ignition\Exceptions\ViewException 试图获取非对象的属性“name_category”

Posted

技术标签:

【中文标题】Facade\\Ignition\\Exceptions\\ViewException 试图获取非对象的属性“name_category”【英文标题】:Facade\Ignition\Exceptions\ViewException Trying to get property 'name_category' of non-objectFacade\Ignition\Exceptions\ViewException 试图获取非对象的属性“name_category” 【发布时间】:2020-09-24 19:20:13 【问题描述】:

我的项目遇到了麻烦.. 我不知道我应该是什么样子!希望其他前辈朋友能帮我解决这个问题。

这是我的控制器

  public function orderPacket($id)
$data_paket = DB::table('list_packet')->where('list_packet.id', $id)->join('d_list_packet', 'list_packet.id', '=', 'daftar_paket_detail.idPacket')->select('daftar_paket_detail.kode_packet as kode_packet')->get();
foreach ($data_packet as $packet) 
  $kode_packet = $paket->kode_packet;
  $data_kate = FD::where('kode_packet', $kode_packet)->value('nama_packet');
  $packet=  DFD::where([
    ['kode_packet', $kode_menu],
    ['status', '1'],
  ])->get();
  $data[] = [
    'name_category' => $data_kate,
    'data_packet' => $packet
  ];
   
return View('user.order-packet',[
  'data' => $data
]);  

模型数据,

name_category = "Packet A",
data_packet = 
    [id = 121,
     kode_packet = PK-204,
     name_packet = Economical 1,
     price = 10.00
   ],
    [id = 111,
     kode_packet = PK-207,
     name_packet = Economical 2,
     price = 12.00
   ],
    [id = 145,
     kode_packet = PK-214,
     name_packet = Economical 3,
     price = 15.00
   ],


name_category = "Packet A",
data_packet = 
    [id = 132,
     kode_packet = PK-204,
     name_packet = Economical 1,
     price = 10.00
   ]

大概是这样

这是我的刀

@foreach($data as $category)
  <div class="col-12 mb-3">
    <label for="">$category->name_category</label>
    <select class="form-control nasi mb-1" name="nasi" >
      <option value="">Pilih Nasi</option>
      @foreach($category->data_packet as $datas)
      <option value="$datas->kode_packet"> $datas->name_packet - number_format($datas->price,'0',',','.') </option>
      @endforeach
    </select>
  </div>
  @endforeach

click for view controller screeenshot click for view controller screeenshot

【问题讨论】:

【参考方案1】:

您创建了一个数组,但您尝试获取一个对象。我认为这是你的问题。 在您的视图中尝试此代码

$category['name_category']

【讨论】:

以上是关于Facade\Ignition\Exceptions\ViewException 试图获取非对象的属性“name_category”的主要内容,如果未能解决你的问题,请参考以下文章