Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnef_sh's avatar

get the most purchases product for current user

I have 3 tables here (order_lists, order_details, products)

Table order_lists contain the following user_id

Table order_details contain the following order_lists_id product_id

Table products contain all the products details.

the three tables are related to each others in models.

I need to select the most purchases items from table order_details based this user_id.

I made very basic Eloquent query which will select 6 products for this user but stuck on how to get the most purchases product

$mostPurchases = OrderList::where( 'user_id', Auth::id() )
                 ->with( [
                   'orderDetails' => function ( $query ) {
                         $query->with( 'productId' );
                    }
                 ] )->take( 6 )->get();

but this didn't work as expected

so I used the DB

DB::table( 'products' )
              ->leftJoin( 'order_details', 'products.id', '=', 'order_details.product_id' )
              ->select( DB::raw( 'count(*) as pro_count, product_id' ) )
              ->leftJoin( 'order_lists', 'order_details.order_lists_id', '=', 'order_lists.id' )
              ->where( 'order_lists.user_id', '=', $usId )
              ->orderBy( 'pro_count', 'DESC' )
              ->groupBy( 'product_id' )
              ->take( 6 )
              ->get();

This working fine but don't get the products details

it just returns the (product_id, and the pro_count)

something like this

        {
            "pro_count": 22,
            "product_id": 733
        },

how can I return the product details with the response.

0 likes
1 reply
jlrdw's avatar

I did not rework your data, but giving another example:

$quy = Powner::query()->leftJoin('dc_pets', 'dc_powners.ownerid', '=', 'dc_pets.ownerid')
                ->select('dc_powners.ownerid', 'dc_powners.oname')
                ->selectRaw('count(dc_pets.petid) as countOfPets')
                ->groupby('dc_powners.ownerid')
                ->orderby('dc_powners.oname')
                ->get();

Results basically give:

ownerid, oname, countOfPets

Like:

5|Bob|3
4|Greg|9
2|Rob|1

Notice the oname column, I had to tell the query to show it.

->select('dc_powners.ownerid', 'dc_powners.oname')
->selectRaw('count(dc_pets.petid) as countOfPets')

notice the regular select,

then came the aggregate raw select

Play with the order of statements they matter. And use an eloquent query, not query builder, they seem to do better for me.

https://laravel.com/docs/5.8/queries

https://laravel.com/docs/5.8/eloquent

https://laravel.com/docs/5.8/eloquent-relationships

I'd suggest viewing some videos on some of these various topics.

And or actually work the examples Taylor provides in the documentation.

Also even if eloquent can't handle a certain complex query, you can also write normal queries in laravel.

And you can query a query:

https://laracasts.com/discuss/channels/eloquent/calculate-average-of-multiple-fields-eloquent

And here is a good example of DB Facade usage:

https://laracasts.com/discuss/channels/laravel/sql-native-to-query-builder

Please or to participate in this conversation.