I'm using Select2 for dynamic drop-down search with Ajax, same "type" (column name) is not grouping.
Tried to group similar column using groupBy() but its retrieving only first row.
As you can see above it's fetching all data perfectly but not grouping.
Controller:
$data = DB::table('vehitrans')
->select("id", "name", "type")
->where('name', 'LIKE', "%$search%")->orWhere('type','LIKE', "%$search%")
->groupBy('type')
->get();
Ajax:
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.type,
children: [{
text: item.name,
id:item.id,
}]
}
})
}
}
I want all "name" should come under its respective "type" eg.
Vehicles
Car
Tractor