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

AbdulRehmanDar's avatar

Wants to create a query.

Hi there. I want to create a query. I have a model PropertyDeal which have columns agent_commision, agency_commision, user_id have more but need these right now. what I want is to display records in the table like this

AGENT	DEALS CLOSE	 DEALS MISSED	COMMISSION
John         5            3          5000
Hamza         9            3          9000

can get close deal by checking how many records are created againts user_id. and get total commission of that user by sum the column agent_commission of that user_id records name can be get from user relation don't want to get missed deals for now because it will be complicate for me to tell where its comming from. Help!

0 likes
11 replies
Sinnbeck's avatar

Not totally sure what you are after

Something like this?

User::withCount('commissions')->withSum('commissions')->get();
AbdulRehmanDar's avatar

@Sinnbeck no it's something like I want to order By which user_id have more records in the table then get the commission of users.

$top_agents = PropertyDeal::with(['user'])
            ->get()
            ->groupBy('user_id');
Sinnbeck's avatar

@abdulrehmandar2234 So for each row in property_deals, you just want that + the user? No summing of the agent_commision or similar?

Maybe explain how each of these columns are calculated

AGENT	DEALS CLOSE	 DEALS MISSED	COMMISSION
John         5            3          5000
AbdulRehmanDar's avatar

@Sinnbeck property deals are basically deals that are closed by any agent which is user_id. I want to display the user name with his total close deals that can be get from how many records are there in a table based on user_id. Then the commission is the sum of all the records created by the user_id which can be get from the sum of the agent_commision column. And also want to order records based of which agent have more agent_commission and close deals

Sinnbeck's avatar

@abdulrehmandar2234 So my query was right? Assuming the relationship is called commissions()

User::withCount('commissions')->withSum('commissions')->orderBy('commissions_count')->get();

$user->commissions_count = the number of commissions

$user->commissions_sum = the sum of commissions

AbdulRehmanDar's avatar

@Sinnbeck no there is no commission relation I explain earlier there are two columns in the table (PropertyDeal Model) user_id, agent_commission which you are getting from the relation but we want the agent_commission column to get the total commission

Sinnbeck's avatar

@abdulrehmandar2234 You said it has agent_commision, agency_commision, user_id.. So user_id isnt related to the user/agent? or is a user not the same as an agent? How is the agent then related?

AbdulRehmanDar's avatar

@Sinnbeck yes user_id is a relation with users table and the agent_commision, agency_commision are decimal which stores commissions you are trying to simplify the query from User model but I want to make a query from PropertyDeal model because from the user first I have to get the company of the user then property then deals which will be complex. Let me explain it again I have a model PropertyDeal you can assume it has only two columns user_id and agent commission. user_id is the relation with the user who created the records and agent_commission(decimal data type not a relation 5000$) is the commission of that user who created the records. A user can have multiple records of PropertyDeal and for each record, the user will have its commission in the row and its user_id. Now the things are the same again get how many deals are created by that user can get from how many records are created by that user_id that's why I used groupBy('user_id') and then sum the agent_commission column based on user_ids. and then order it by which user have more commission and records created by the user. Want to solve this with PropertyDeal Model not with User Model Thank You for the corporation.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@abdulrehmandar2234 Why do you want to start from the property model? My example should give you exactly what you want. Have you even tried it?

$users = User::withCount('commissions')->withSum('commissions')->orderBy('commissions_count')->get();
dd($users->first()->commissions_count,$users->first()->commissions_sum); //what does this give you

It sounds like you wish to extract every single commission from the database, and do the calculations in php, which is bad practice. Let the database handle it if it can.

AbdulRehmanDar's avatar

@Sinnbeck Thank You This worked just created a user relation with a property deal

$top_agents = User::withCount('propertyDeals')->withSum('propertyDeals', 'agent_commission')->orderBy('property_deals_count', 'DESC')->get();

Don't know why I'm not trying to do it with User Model Thank You!

Please or to participate in this conversation.