Hello @zoltan007
I don't see the relations defined in your code but I think you need something like this:
$productId = 123; // id of your product
$userId = 567; // id of current user
// we check if the is an order for the current user that has an order detail for the product
$hasOrderedProduct = Order::where('user_id', $userId)
->whereHas('orderDetails', static function ($orderDetailQuery) use ($productId) :void {
$orderDetailQuery->where('product_id', $productId);
})
->count() > 0; // more then 0 means it exist, we don't do a get to prevent that we load a lot of unwanted data.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community