Reputation: 491
What i am trying to do is get data from table (it is ajax request), sort and send to view. Want show in select box options. I have to change id with value to make JS sort by value.
Quick example
ModelVehicle::where('make_id', $request->make_id)->orderBy('name')->pluck('id', 'name');
and JS
$("#model_id").empty();
$("#model_id").append(new Option(data.please_select, 0));
$.each(data.models, function(index, item) {
$("#model_id").append(new Option(index, item));
});
In this case this is exactly what i need but now i need use map cause i am using language file and i have code below
Category::find($request->category_id)->orderBy('public_name')->pluck('id', 'public_name')->map(function ($key, $item) {
return __('general.' . $item . '');
})
but the result is
I tried modify map func but with no result. Is any way to do this on backend side ? or should i use JS to sort and modify my array ?
Upvotes: 0
Views: 437
Reputation: 8252
I am not sure if I understand your problem correctly. Form what I understand the problem is that you get a collection with arrays ['id', 'public_name']
instead of ['public_name', 'id']
.
You can't manipulate the key with map
but what you could do is this:
Category::find($request->category_id)->orderBy('public_name')->pluck('public_name', 'id')->map(function ($key, $item) {
return __('general.' . $key . '');
})->flip();
What this does is create a collection of arrays ['id', 'public_name']
, then the map
replaces the values with the translation and afterwards we flip
the values and keys so we have ['public_name', 'id']
.
Upvotes: 1