【问题标题】:Laravel - Can't access pivot table dataLaravel - 无法访问数据透视表数据
【发布时间】:2015-01-10 21:31:15
【问题描述】:

我很难让 Eloquent 玩得好;我的查询似乎总是有问题。无论如何,我有一个类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');
        }
}

我正在尝试从给定Item 的数据透视表中获取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-&gt;length-&gt;first()-&gt;pivot 是什么给了我错误:“尝试获取非对象的属性”。搜索了几个小时后,看来这种语法应该是可以接受的,$item-&gt;length-&gt;first()-&gt;pivot-&gt;value,但我无法让它工作。我也尝试过这种方式,$item-&gt;length-&gt;pivot-&gt;value,但是当然,它不起作用,因为我已经定义了两者之间的多对多关系。

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

下面是var_dump($item-&gt;length-&gt;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

【问题讨论】:

  • 您是否有可能没有分配Length 的项目?所以$item-&gt;length-&gt;first() 会返回null?
  • :facepalm: 这正是问题所在!我不知道为什么我没有想到。感谢您的帮助。

标签: sqlite laravel laravel-4 eloquent pivot-table


【解决方案1】:

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

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

【讨论】:

  • 是的,我肯定会这样做,一旦我进一步充实了我的应用程序。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 2022-06-20
  • 2015-09-21
相关资源
最近更新 更多