Reputation: 263
I have my model, controller and database migration already working and I'm able to retrieve display the data.
clients.blade.php
@foreach ($table_companies as $client)
{{ $client['geography'] }} - {{ $client['company'] }} - {{ $client['country'] }} - {{$client['industry'] }}
@endforeach
ClientsController.php
public function showClients() {
$table_companies = Clients::selectRaw(' name_company company, geography geography, country country, type_industry industry, emphasize bold')
->groupBy('company', 'geography', 'country', 'industry', 'bold')
->get()
->toArray();
return view('about.our-clients', compact('our-clients', 'table_companies'));
2017_12_12_173246_create_clients_table.php
Schema::create('table_companies', function (Blueprint $table) {
// $table->increments('id');
// $table->timestamps();
$table->increments('ID_CLIENT');
$table->string('NAME_COMPANY');
$table->string('TYPE_INDUSTRY');
$table->string('COUNTRY');
$table->string('GEOGRAPHY');
$table->integer('emphasize');
$table->timestamps();
});
}
What I'm trying to do is to format the data and group it by geography passing url parameter
Example 1: /our-clients.php?geography=North_America
And group them like:
NORTH AMERICA USA
CANADA Frontier | Federal Court
Example 2: /our-clients.php?geography=Europe
EUROPE
BELGIUM
DENMARK Skandina | National Police
Any ideas?
Upvotes: 1
Views: 565
Reputation: 1037
It reads as if you want to filter for geography? Or group by? But if it's the latter, why do you have that get parameter? Not entirely clear.
So if you wanna filter, you should obviously include a where('geography' , request()->geography) in your query.
You could also get all and filter afterwards with the filter() method
https://laravel.com/docs/5.5/collections#method-filter
$filtered_table_companies = $table_companies->filter(function ($value, $key) {
return $value->geography == request()->geography;
});
I would suggest the Eloquent version, if you don't need the other regions anywhere else.
If you want to group by country, I would then suggest
https://laravel.com/docs/5.5/collections#method-maptogroups
$grouped_table_companies = $table_companies->mapToGroups(function ($item, $key) {
return [$item->geography] => $item;
});
Try this in tinker once to see the output of it and try to understand it.
You can then go through that collection with 2 nested foreach in your blade file like
@foreach($grouped_table_companies as $region => $regional_table_companies)
<div>{{ $region }}</div>
@foreach($regional_table_companies as $table_company)
// output stuff here
@endforeach
@endforeach
If you wanted a groupby early on instead of a filter, you can obviously perform 2 mapToGroups() in your process, just play around a tinker for a bit ;)
Upvotes: 1
Reputation: 9853
Use groupBy()
after getting all records to get it as a group of records
associate with geography
.
$table_companies = Clients::selectRaw(' name_company company, geography geography, country country, type_industry industry, emphasize bold')
->get()
->groupBy('geography')
->toArray();
Upvotes: 1
Reputation:
You would change your groupBy
clause to use request parameters:
...
->groupBy('company', request()->get('geography'), 'country', 'industry', 'bold')
->get()
->toArray()
Upvotes: 1