Can you give an example of the data you have in the tables, and show the expected result?
The description you have given just makes me confused as to what you are trying to do.
Hello Community,
I can't figure out how to achieve my goal:
My orders always have a main user and can have attached Users. All users have order products, which are connected to users and the order itself via an id for each.
Now I want to load all orders from a specific main user: $userID, where the payment has status 2,5 or 6, all order products for the main user and all attached users with their order products, where the order_id inside the orderProduct is the same as the current query-order_id.
It always only loads the order specific attached users but unfortunately all of their OrderProducts (also those who are connected to an earlier order).
Attached Users are connected to orders via a pivot table,
Any Idea how to fix this? Is there a possibility to use the "current order_id" which is queried inside the "order product filter subquery"?
$orders = Order::where(function ($query) use ($userID) {
return $query->where('store_user_id', $userID);
})->whereHas('payments', function ($query) {
return $query->where('payment_status_id', 2)
->orWhere("payment_status_id",5)
->orWhere("payment_status_id",6);
})->with(['orderProducts',
'payments',
// load attached users with order products, where the order product id is the same as the current order_id
'attachedUsersPivot' => function($query) {
$query->with('orderProducts')
->whereHas('orderProducts', function ($subQuery) {
$subQuery->whereColumn('order_products.order_id', 'auser_order.order_id');
});
},
'discounts',
'invoices',
'orderStatus',
'shippingStatus'])
->orderBy('id', 'DESC')
->get();```
## Inside the Attached User Model:
`public function orderProducts(): HasMany
{
return $this->hasMany(OrderProduct::class,"attached_user_id");
}`
In the end I want to pass the `$orders` variable to my view and iterate over each attachedUser and also retrieve his items via $attachedUser->orderProducts
Please or to participate in this conversation.