我试图在线获取非对象错误的属性:<span>{{$item->subcats->name}}</span>
。如何获得产品子标识?
我的产品型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $guarded=[];
public function subcats(){
return $this->belongsTo(Subcat::class);
}
public function comments(){
return $this->hasMany(Comment::class);
}
}
my Subcat Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Subcat extends Model
{
protected $guarded=[];
public function cats(){
return $this->belongsTo(Cat::class);
}
public function products(){
return $this->hasMany(Product::class);
}
}
the blade is:
@extends('template.master')
@section('content')
@forelse($product as $item)
<div class="col-lg-4">
<a href="/product/{{$item->id}}">{{$item->title}}</a><br>
<span class="caret">{{$item->description}}</span><br><br>
<span>Price :</span><span class="caret">{{$item->price}}</span><br>
<span>{{$item->subcats->name}}</span>
</div>
@empty
@endforelse
@endsection
答案 0 :(得分:0)
您指的是错误的{{ $item->subcats->name}}
答案 1 :(得分:0)
我应该将Product.php中从subats到subcat的方法的名称更正。
public function subcat(){
return $this->belongsTo(Subcat::class);
}
然后使用:
@foreach($product as $item)
{{$item->subcat->name}}
@endforeach