Laravel 属性 [title] 在此集合实例上不存在

标签 laravel

我正在尝试从第三个表收集我的产品数据,我得到了

Property [title] does not exist on this collection instance.

逻辑

  1. custom_product_specifications 表获取 product_idspecation_idtext_declongtext_dec
  2. 如果用户正在访问的产品在 custom_product_specifications 中具有 ID,我将根据其 ID 显示规范标题
  3. 我将显示 text_declongtext_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}} 这就是我得到的:

screenone

我的其中一个数据具有规范信息(重复),其他数据仅显示 []

更新

根据下面的答案,我将我的 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 个问题:

  1. 我无法获取所有数据
  2. 它们不会内爆(不在规范中,也不在细节中)

屏幕截图

我的数据库

dbscreen

PS:它只会加载我最后的输入,即 id:4

Blade 结果

bladescreen

PS:我的输入 id:4 重复 3 次,而我应该得到具有不同信息的 4 结果。

最佳答案

相关的CustomProductSpecificationSpecification属于许多关系。您必须使用此代码

@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/

相关文章:

sql - 将原始查询转换为 Laravel eloquent

在 Laravel 5.5 中更新代码时,php artisan serve 被缓存或没有反应。重新启动后,它每次都会再次运行

php - Laravel 模型函数内的联合

laravel - Vagrant - Homestead 设置多个站点

laravel - PDOException (1044) SQLSTATE[HY000] [1044] 用户 '' @'localhost' 对数据库 'forge' 的访问被拒绝

php - 不同 View 组的自定义错误页面

Laravel 设置引导选择

javascript - Laravel 星级评分,字体很棒

javascript - 使用 React-Laravel 相对于普通 React 和 Laravel 的优缺点

mysql - laravel中如何将图片上传到数据库