首页 文章

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

提问于
浏览
1

我的查询总是出错了 . 无论如何,我有一个 Item 类,通过 r_item_length 表与 Length 有多对多的关系 . 在数据透视表中,还有一个 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');
        }
}

我试图从枢轴表中获取给定 Itemvalue . 目前,我正试图在我的视图中访问它,如下所示:

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 是什么给了我错误:“ Trying to get property of non-object ” . 搜索几个小时后,看起来这个语法应该是可以接受的,但是我也可以这样尝试,但是当然,它并没有定义两者之间的多对多关系 .

这是否表明我的模型设置方式有问题?我只是错误地查询它吗?谢谢你的帮助 .

以下是 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

1 回答

  • 3

    问题恰好是某些 Item 没有分配 Length . 所以 $item->length->first() 为空,因此访问 pivot 会引发错误 . 解决方案非常简单 . 只需缠绕它就可以了,你应该没问题 .

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

相关问题