Right now I have below data:
Now the problem with above is, that it's creating a new array for each entry in my database.
I would very much like to get below output:
The difference is here that:
- All the routes that are the same, should be grouped. So if the
Route
is the same, it should be under the same route list. If the
Carrier
under the route is the same, then it should be under that route and carrierIf the route is different, it should start a new list (**Route: XYZ to XYZ)
Current Code:
Controller.php
:
public function show()
{
$routes = Routes::where('id', '>=', 1)
->with('routeStops', 'getVessel')
->get()
->toArray();
foreach ($routes as $key => $value) {
echo '<h2>Route: ' . $value['origin'] . ' to ' . $value['destination'] . '</h2>';
$carrier = Carriers::where('id', $value['get_vessel']['carriers_id'])->first()->name;
echo 'Carrier: ' . $carrier . '';
dump($value);
}
}
This is my models:
Routes.php
/**
* Get the route stop associated with this route.
*/
public function routeStops()
{
return $this->hasMany('App\RouteStops', 'route_id');
}
/**
* Get the vessel record associated with this route.
*/
public function getVessel()
{
return $this->belongsTo('App\Vessels', 'vessel_id');
}
How can I do so I achieve the 2nd desired output?