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!
@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 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
@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?
@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.
@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.