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

lewis4u's avatar

Query with one to many relationship

This is a very common situation with DB and I just want to learn how do you guys solve this type of problem using Laravel?

Let's say we have a common Model User and one more model WorkHour.

Relationship User has many WorkHours.

users table: id, name, email

there are 10.000 users in this table

workhours table: id, user_id, start_time, end_time, hours_worked -> in seconds

there are more than 3.000.000 records in workhours table

And let's say we want to show a table in browser with just 2 columns: Username and WorkHoursSum

https://s26.postimg.org/cihk1d5xl/Screenshot_from_2017-09-04_20-23-21.png

How do you make the query in backend?

0 likes
8 replies
jlrdw's avatar

Sounds like it's for an admin / report. I'd dump data to local machine, already have an ODBC connection setup and do the report in either visual foxpro or ms access. I did monthly A/R, A/P reports like that at a trucking company.

A desktop database is far more efficient for large reports and that sort of thing.

Just the way I do things.

Edit: I hope you don't want all 3 million at one time, normally reports are ran weekly / monthly with just those results, and in case of A/R any not paid yet.

Either way look at chunking results if needed.

lewis4u's avatar

OK now I might have some temporary solution for this problem...

What if I have a column 'total_work_hours' in users table and update that value each time when new work_hours record is inserted or deleted or updated...

Then I won't have to calculate the work hours when I want to show the total_work_hours for each user?

Is this a bad thing to do?

jlrdw's avatar

The answer is only query whats needed. If you really want to query 3 million records often, good luck. Even paginated 20 per page, most people aren't going to paginate through all that mess.

You need to roll up your sleeves and get to work on queries that summarizes the data for the admin / chief / or whoever.

A group by would be a starting point.

The ORM isn't the way to go here.

If your application is going to be that huge you had better starting learning enterprise java programming, or for php compiled c++ classes.

I just want to learn how do you guys solve this type of problem using Laravel

A large enterprise application isn't solved with laravel or php.

Edit: Fedex requirements

* Java – J2EE, JDBC, JMS, EJB’s, etc
*  ICE Faces
 * Web Services – WSDL / SOAP
 * Weblogic
 * Eclipse
*  Linux & Shell Scripting
*  SQL
 * Hibernate
 * Spring
 * SVN Source control
 * ANT and Maven
*  AngularJS and HTML5
lewis4u's avatar

My problem is that I'm using datatables and i need to load everything so it can be sortable in browser... but you are probably right i still need to learn a lot

jlrdw's avatar

@lewis4u forget you ever heard the word datatables for huge results, stick to normal html table with normal pagination. Php or blade either, but only query what's needed.

Edit: quick lesson

There's a huge difference in a year end printed report and viewing on screen.

So if Boss only needs to see information on Bob Johnson, have a query pull up Bob Johnson with the information between two dates.

Boss not interested in Mary Adkins when viewing Bob.

Try to just pull what's needed (paginated) and you will be fine.

lewis4u's avatar

What about sorting? this is the only reason I am using datatables

1 like
jlrdw's avatar

You can have table headers as links, whereby user clicks say lastname and via a route, have a controller method order by last name.

For learning purpose, install cakephp 3.* and do a cake bake to return controller model view, and you will see how it works. But really it's just a controller method to order by a certain field.

I'm not saying don't use datatables, I am saying no need to query 3 million results at once. Bob in example above surely doesn't have 3 million records.

Please or to participate in this conversation.