Support the ongoing development of Laravel.io →
Blade Views
Last updated by @zoltan007 1 year ago.
0
moderator

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.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.