Laravel-迭代数据透视表(此集合实例上不存在属性)

时间:2018-12-07 21:25:48

标签: laravel laravel-5 eloquent

我在OrderProduct之间有多对多关系

Product.php

public function orders()
{
    return $this->belongsToMany(Order::class)
                ->withTimestamps()
                ->withPivot('qty');
}

Order.php

public function products()
{
    return $this->belongsToMany(Product::class)
                ->withTimestamps()
                ->withPivot('qty');
}

现在,每当我尝试在视图中利用迭代时(在这种情况下,我只是试图显示遍历所有可用Product的形式,我总是会收到错误信息...

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

create.blade.php

@foreach ($products->orders as $product)

# Order inputs are here

{{ Form::text('qty', $product->pivot->qty, [
                          'type' => 'tel',
                            'min' => 0,
                            'max' => 20,
                            'step' => 1,
                            'placeholder' => '0',
                            'class' => 'meal'
                            ]) }}
@endforeach

我也尝试过@foreach ($products->orders as $product),两种方法都给我同样的错误。

我在控制器中尝试了多种方法来解决此错误,这是我最后一次尝试:

OrderControlller.php

public function create()
{
    $user = Auth::user();
    $products = Product::get();
    $orders = $user->orders;

    return view('orders.create', compact('products', 'orders', 'user'));
}

更新

@alan的答案是正确的,但是,我确定...

每当我尝试运行迭代时,我仍然会收到“此集合实例上不存在属性[pivot]”。

在这种情况下,在迭代内部进行迭代的概念令我感到困惑。

我无法想象Laravel如何处理枢轴连接。在修补程序中,当我仅加载产品表时,没有qty列。 (这很有意义,因为它在数据透视表上)。这也解释了这个新错误。

我应该做些什么吗? :

更改了create.blade.php

@foreach ($products as $product)
 {{ Form::text('qty', $product->orders->pivot->qty }}

OrderController.php

$user = Auth::user();
$orders = $user->orders;
$products= []; #pass products to view as an array
$p = $orders->products; #this relationship brings in pivot data? 
foreach ($p as $orders) {
   #would I then somehow pass just this qty here?
}

问题是我总是收到“属性不存在”错误,可能是“产品”,“订单”或“数据透视”。

1 个答案:

答案 0 :(得分:2)

这应该有效。您试图访问$ products变量上的orders属性,该变量是Laravel集合(模型上的get方法返回一个集合)。因此,您不必进行遍历产品并从各个产品模型访问数据透视表的方式。

@foreach ($products as $product)

# Order inputs are here

{{ Form::text('qty', $product->orders->pivot->qty, [
                          'type' => 'tel',
                            'min' => 0,
                            'max' => 20,
                            'step' => 1,
                            'placeholder' => '0',
                            'class' => 'meal'
                            ]) }}
@endforeach

更新: 其实这是有道理的。数据透视表上的记录定义了订单和产品之间的关联。因此,要访问数据透视表中的记录,必须从产品或订单的关系中访问产品或订单。这就是我要做的。

OrderController.php

$user = Auth::user();
$user->load("orders.products"); // eager load relationship to avoid N+1 problem
$orders = $user->orders;
return view('orders.create', compact('orders', 'user'));

create.blade.php

@foreach ($orders as $order)
    @foreach ($order->products as $product)


    {{ Form::text('qty', $product->pivot->qty, [
                          'type' => 'tel',
                            'min' => 0,
                            'max' => 20,
                            'step' => 1,
                            'placeholder' => '0',
                            'class' => 'meal'
                            ]) }}
     @endforeach
@endforeach

一些资源:

Eager loading

Many to Many relations