Laravel - 无法访问数据透视 table 数据

Laravel - Can't access pivot table data

我很难让 Eloquent 好好表现;似乎我的查询总是有问题。无论如何,我有一个 class Item 通过 r_item_length table 与 Length 的多对多关系。在枢轴 table 中,还有一个 value 字段。这是 Item 模型:

models/db/Item.php:

class Item extends Eloquent implements UserInterface, RemindableInterface {
    ...

    protected $fillable = ['item_singular', 'item_plural'];

    public function length() 
        {
            return $this->belongsToMany('Length', 'r_item_length')->withPivot('value');
        }
}

我正在尝试从给定 Item 的枢轴 table 中获取 value。目前,我正在尝试像这样在我的视图中访问它:

views/db/show.blade.php

<body>
    <table>
    @foreach ($items as $item)
        <tr class="record">
            <td>{{ $item->id }}</td>
            <td>{{ $item->item_singular }}</td>
            <td>{{ $item->item_plural }}</td>
        </tr>

        <tr>
            <td>{{ var_dump($item->length->first()->pivot->value) }}</td>
        </tr>
    @endforeach
    </table>
</body>

$item->length->first()->pivot 是给我错误的原因:“试图获取非对象的 属性”。搜索了几个小时后,似乎这种语法应该是 acceptable、$item->length->first()->pivot->value,但我无法让它工作。我也试过这种方式,$item->length->pivot->value,当然不行,因为我定义了两者之间的多对多关系

这是否表明我的模型设置方式有问题?我只是查询不正确吗?感谢您的帮助。

下面是 var_dump($item->length->first()).

第一项的输出
object(Length)[167]
  protected 'table' => string 'length' (length=6)
  protected 'hidden' => 
    array (size=0)
      empty
  protected 'fillable' => 
    array (size=3)
      0 => string 'length_short' (length=12)
      1 => string 'length_long_singular' (length=20)
      2 => string 'length_long_plural' (length=18)
  protected 'connection' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'perPage' => int 15
  public 'incrementing' => boolean true
  public 'timestamps' => boolean true
  protected 'attributes' => 
    array (size=6)
      'id' => string '2' (length=1)
      'length_short' => string 'ft' (length=2)
      'length_long_singular' => string 'Foot' (length=4)
      'length_long_plural' => string 'Feet' (length=4)
      'created_at' => string '2015-01-10 11:00:49' (length=19)
      'updated_at' => string '2015-01-10 11:00:49' (length=19)
  protected 'original' => 
    array (size=9)
      'id' => string '2' (length=1)
      'length_short' => string 'ft' (length=2)
      'length_long_singular' => string 'Foot' (length=4)
      'length_long_plural' => string 'Feet' (length=4)
      'created_at' => string '2015-01-10 11:00:49' (length=19)
      'updated_at' => string '2015-01-10 11:00:49' (length=19)
      'pivot_item_id' => string '1' (length=1)
      'pivot_length_id' => string '2' (length=1)
      'pivot_value' => string '1.2' (length=3)
  protected 'relations' => 
    array (size=1)
      'pivot' => 
        object(Illuminate\Database\Eloquent\Relations\Pivot)[172]
          protected 'parent' => 
            object(Item)[165]
              ...
          protected 'foreignKey' => string 'item_id' (length=7)
          protected 'otherKey' => string 'length_id' (length=9)
          protected 'guarded' => 
            array (size=0)
              ...
          protected 'connection' => null
          protected 'table' => string 'r_item_length' (length=13)
          protected 'primaryKey' => string 'id' (length=2)
          protected 'perPage' => int 15
          public 'incrementing' => boolean true
          public 'timestamps' => boolean false
          protected 'attributes' => 
            array (size=3)
              ...
          protected 'original' => 
            array (size=3)
              ...
          protected 'relations' => 
            array (size=0)
              ...
          protected 'hidden' => 
            array (size=0)
              ...
          protected 'visible' => 
            array (size=0)
              ...
          protected 'appends' => 
            array (size=0)
              ...
          protected 'fillable' => 
            array (size=0)
              ...
          protected 'dates' => 
            array (size=0)
              ...
          protected 'touches' => 
            array (size=0)
              ...
          protected 'observables' => 
            array (size=0)
              ...
          protected 'with' => 
            array (size=0)
              ...
          protected 'morphClass' => null
          public 'exists' => boolean true
  protected 'visible' => 
    array (size=0)
      empty
  protected 'appends' => 
    array (size=0)
      empty
  protected 'guarded' => 
    array (size=1)
      0 => string '*' (length=1)
  protected 'dates' => 
    array (size=0)
      empty
  protected 'touches' => 
    array (size=0)
      empty
  protected 'observables' => 
    array (size=0)
      empty
  protected 'with' => 
    array (size=0)
      empty
  protected 'morphClass' => null
  public 'exists' => boolean true

问题恰好是某些 Item 没有分配 Length。所以 $item->length->first() 为空,因此访问 pivot 会引发错误。这个问题的解决方案相当简单。只需在它周围加上一个 if 就可以了。

<tr>
    @if($length = $item->length->first())
        <td>{{ $length->pivot->value }}</td>
    @endif
</tr>