Q:

laravel show two tables data grouped as one

I have two tables named paths and stoppages, structured as:
PATHS:
_____________________________
| id | source | destination |
|____|________|_____________|
|  1 |   a    |    b        |
|____|________|_____________|
|  2 |   a    |    c        |
|____|________|_____________|
|  3 |   d    |    e        |
|____|________|_____________|

STOPPAGES:
________________________
| id | path_id | point |
|____|_________|_______|
|  1 |   1     |   p1  |
|____|_________|_______|
|  1 |   1     |   p2  |
|____|_________|_______|
|  1 |   1     |   p3  |
|____|_________|_______|
|  1 |   2     |   p4  |
|____|_________|_______|
|  1 |   2     |   p5  |
|____|_________|_______|
|  1 |   3     |   p1  |
|____|_________|_______|

My query to get data from two table is
DB::table('paths')->join('stoppages', 'paths.id', '=', 'stoppages.path_id')->get()

I want to show them in laravel blade page in one table. Currently my table look like this:
________________________________________
| source | destination |   stoppages   |
|________|_____________|_______________|
|   a    |     b       |       p1      |
|________|_____________|_______________|
|   a    |     b       |       p2      |
|________|_____________|_______________|
|   a    |     b       |       p3      |
|________|_____________|_______________|
|   a    |     c       |       p4      |
|________|_____________|_______________|
|   a    |     c       |       p5      |
|________|_____________|_______________|
|   d    |     e       |       p1      |
|________|_____________|_______________|

But I want to show them in following format?
________________________________________
| source | destination |   stoppages   |
|________|_____________|_______________|
|   a    |     b       |   p1, p2, p3  |
|________|_____________|_______________|
|   a    |     c       |     p4, p5    |
|________|_____________|_______________|
|   d    |     e       |       p1      |
|________|_____________|_______________|

How to show those data in my desired format?

A:

GROUP_CONCAT function returns a string result with the concatenated non-NULL values from a group

You can use groupBy and group_concat like this:
DB::table('paths')
  ->join('stoppages', 'paths.id', '=', 'stoppages.path_id')
  ->groupBy('source', 'destination')
  ->select('source', 'destination', DB::raw('GROUP_CONCAT(stoppages separator ", ") AS stoppages'))
  ->get()

