首页 文章

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

提问于
浏览
1

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) ,两种方法都给了我同样的错误 .

我在Controller中尝试了许多不同的方法来修复此错误,这是我的最后一次尝试:

OrderControlller.php

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

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

UPDATE

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

每当我尝试运行迭代时,我仍然会在“此集合实例上存在”Property [pivot] .

在这种情况下,迭代内部迭代的概念让我感到困惑 .

我无法想象Laravel如何处理枢轴连接 . 在我只加载Product表时,修补程序没有 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?
}

问题是我总是得到“ property 不存在”错误,无论是“产品”,“订单”还是“枢轴” .

1 回答

  • 2

    这应该工作 . 您试图访问$ products变量上的orders属性,这是一个Laravel Collection(模型上的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
    

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

    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

相关问题