I have a table
$builder->on('gift_sents.receiver_id', '=', 'users.id');
->select('users.*', DB::raw('COUNT(gift_sents.receiver_id) as total_posts'))
First you will have to define relationship with User and GiftSent Model. Get the sender_id who has sent maximum count of gifts. Fetch all the user using relation ('with').
App\GiftSent::with('userRelationName') ->select('sender_id', DB::raw('COUNT(*) as giftCount')) ->groupBy('sender_id') ->orderBy(DB::raw('COUNT(*)')) ->get();
Other solution is to use join but it will be complicated and costly.