Support the ongoing development of →
Blade Views
Last updated by @zoltan007 5 months ago.

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!

Full Stack Europe

Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.