Laravel join with count

can anyone help me with Laravel join with count I’m trying to get the laravel to join with a count of three tables where table 1 one is a relationship with table 2 and table 3 Which display data of table1 and table and the count value of table three only with a relationship with table1

Table name: table1

Id name

1 a1

2 a2

3 a3

table name : table2

id 2name table1_id

1 b1 1

2 b2 2

3 b3 3

table name: table3

id 3name table1_id

1 c1 1

2 c2 1

3 c3 2

4 c4 2

4 c5 3

Output need

Name 2name count3name

a1 b1 2

a2 b2 2

a3 b3 1


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your best bet would be to setup a relationship for table2 and table3 in your table1 model. When you do that you can use the relationship to get the count. For example, you’d have something like this in your table1 model:

public function table3()
    return $this->hasMany(Table3::class);

Then in your view, you’d just echo $table1->table3->count()

Check out the relationship docs:

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x