mardi 19 novembre 2019

How to get distinct record through many to many relation

I have following pivot table.

+----+-----------+-----------+
| id | tableA_id | tableB_id |
+----+-----------+-----------+
|  1 |         1 |         1 |
|  2 |         1 |         2 |
|  3 |         1 |         3 |
|  4 |         2 |         1 |
|  5 |         2 |         4 |
+----+-----------+-----------+

Controller:

$tableAs = \App\TableA::all();

Many to many relation are already defined in the model, and view code is following.

@foreach ($tableAs as $tableA)
@foreach ($tableA->$tableBs as $tableB)
<p></p>
@endforeach
@endforeach

Now, I want to get distinct rows for table B.
If above table, the current output is:

1 - 1
1 - 2
1 - 3
2 - 1 // <- not expect
2 - 4

What is the best practice to achieve my hope without writing logic in the view file?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire