Reputation: 411
in My Larvel 5.6 app I am working with mysql db. and in my application I have vehicle table with following columns, vehicles,
id category number brand model
1 car 123 bmw 520d
2 van 258 ford mark
3 car 256 benz ultra
4 car 259 bmw 520d
etc
and I am going to group all models of the table and printing here as My controller,
public function modelstatic()
{
$models = Vehicle::groupBy('modelname')->select('id', 'modelname', \DB::raw('COUNT(*) as cnt'))->get();
return view('modelreports.modelstatic')->withModels($models);
}
my printing blade file is this,
@foreach($models as $model)
{{ $model->modelname }}
@endforeach
it is printing well as
520d
mark
ultra
now I need print in-front of model name there brand name as
520d - bmw
mark - ford
ultra - benz
Then how can I do this now? I have separate Models for brand and model and still there are not relationship between Models. how can I do this, may I need relationship or may I could print brand name via vehicle table data?
Upvotes: 1
Views: 3792
Reputation: 943
The same way you select the model name column, you must also select the brand column, and then print the model with his brand name.
Try this:
In your controller:
public function modelstatic(){
$models = Vehicle::groupBy('modelname')->select('id', 'modelname', 'brand', \DB::raw('COUNT(*) as cnt'))->get();
return view('modelreports.modelstatic')->withModels($models);
}
and in your view:
@foreach($models as $model)
{{ $model->modelname }} - {{ $model->brand }}
@endforeach
Upvotes: 2