我正在尝试从第三个表收集我的产品数据,我得到了
Property [title] does not exist on this collection instance.
逻辑
custom_product_specifications
表获取product_id
,specation_id
、text_dec
和longtext_dec
- 如果用户正在访问的产品在
custom_product_specifications
中具有 ID,我将根据其 ID 显示规范标题
。 - 我将显示
text_dec
和longtext_dec
的数据
代码
自定义产品规范模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CustomProductSpecification extends Model
{
public $timestamps = false;
protected $fillable = [
'product_id', 'specification_id', 'text_dec', 'longtext_dec',
];
public function products(){
return $this->belongsToMany(Product::class);
}
public function specifications(){
return $this->belongsToMany(Specification::class,'custom_product_specifications', 'specification_id');
}
}
产品型号
public function customsubspecifications(){
return $this->belongsToMany(CustomProductSpecification::class, 'custom_product_specifications', 'product_id', 'specification_id');
}
public function specifications(){
return $this->belongsToMany(Specification::class);
}
规范型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Specification extends Model
{
protected $fillable = [
'title',
];
public function subspecifications(){
return $this->hasMany(Subspecification::class);
}
public function sets(){
return $this->belongsToMany(Set::class);
}
public function customproductspecifications(){
return $this->belongsToMany(CustomProductSpecification::class);
}
}
这是我在 View 中获取数据的方法
Controller
$product = Product::where('slug', $productslug)->firstOrFail();
$customspecs = CustomProductSpecification::where('product_id', '=', $product->id)->get();
Blade
@if(!empty($customspecs))
@foreach($customspecs as $customspec)
<tr>
<th style="width:150px;">{{ $customspec->specifications->title }}</th>
<th class="text-left">
{{$customspec->text_dec}}
{{$customspec->longtext_dec}}
</th>
</tr>
@endforeach
@endif
结果
如果我使用 {{ $customspec->specifications->title }}
我会收到错误。
如果我使用 {{ $customspec->specifications}}
这就是我得到的:
我的其中一个数据具有规范信息(重复),其他数据仅显示 []
更新
根据下面的答案,我将我的 blade
代码更改为:
@if(!empty($customspecs))
@foreach($customspecs as $customspec)
@foreach($customspec->specifications as $specification)
<tr>
<th style="width:150px;">{{ $specification->title }}</th>
<th class="text-left">
@isset($customspec->text_dec)
{{$customspec->text_dec}}
@endisset
@isset($customspec->longtext_dec)
{{$customspec->longtext_dec}}
@endisset
</th>
</tr>
@endforeach
@endforeach
@endif
这里有 2 个问题:
- 我无法获取所有数据
- 它们不会内爆(不在
规范
中,也不在细节
中)
屏幕截图
我的数据库
PS:它只会加载我最后的输入,即 id:4
Blade 结果
PS:我的输入 id:4
重复 3
次,而我应该得到具有不同信息的 4
结果。
最佳答案
与相关的
关系。您必须使用此代码CustomProductSpecification
和Specification
属于许多
@if(!empty($customspecs))
@foreach($customspecs as $customspec)
<tr>
@foreach($customspec->specifications as $specification)
<th style="width:150px;">{{ $specification->title }}</th>
@endforeach
<th class="text-left">
{{$customspec->text_dec}}
{{$customspec->longtext_dec}}
</th>
</tr>
@endforeach
@endif
关于Laravel 属性 [title] 在此集合实例上不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51453984/