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

peterpan26's avatar

4 tables sql "with" query

hello i have 4 tables tableOne : ID, CARPLATE, REPAIR_COST tableTwo: ID, CARPLATE, PORTICS_COST tableThree: ID, CARPLATE, FUEL_COST tableFour: ID,CARPLATE, DRIVER_NAME

how do i connect the tables by "CARPLATE" on models, and make a query of the sum of the cost fields and group them by the carplate in a show.blade.php ? i have been trying but i'm not being able, i suppose it starts with:

public function show()
    {
        $summaryData = tableFour::with('tableOne')
        ->leftjoin('tableThree', 'tableFour.CARPLATE', '=', 'tableThree.CARPLATE')
        ->get();
        return view('admin.form.show', compact('summaryData'));
    }

please give me example code as i'm having really difficulty on trying to go over concept to the practice.

0 likes
3 replies
vincent15000's avatar

I don't understand the structure of your database.

What are you trying to do ? Why 4 different tables ?

peterpan26's avatar

@vincent15000 they represent 4 modules on the website where can have data entry there. i want to do a sql with query to display the sum of the REPAIR_COST,PORTICS_COST,FUEL_COST and group them by CARPLATE

Tray2's avatar

@peterpan26 To join more than two tables together you just add additional joins

DB::table('books')
    ->join('formats', 'books.format_id', '=', 'formats.id')
	->join('genres', 'books.genre_id', '=', 'genres.id')
    ->select('books.*', 'formats.format', 'genres.genre)
    ->get();

You can read about it in this post

https://tray2.se/posts/database-design-part-2

Please or to participate in this conversation.