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

peterpan26's avatar

with->hasmany->belongsto separate query and another table

hello i want to know how can i make a separate subtotal of a field common in 3 tables and connect it to the view (the tables) , i've already connected tableOne to tableTwo with the command with

public function tableOne()
    {
        return $this->belongsTo(tableOne::class);
    }
and in the controller i guess that is eloquent : tableTwo::with('tableOne')->get()

now i need to know how to add more one table and make arithmetic query , can be sum for the sake of the example. thanks

0 likes
2 replies
Tray2's avatar
Tray2
Best Answer
Level 73

To join three table and make some arithmics on the columns you should use a join, which is covered here https://tray2.se/posts/database-design-part-2

To make the calculations you just do standard math + - * / depending on what you need to calculate. If you want to do math with more than one row you need to use aggregate functions and group bys, how to do that can be found here https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html

Please or to participate in this conversation.