'how sort by name in json response
Simple thing i am taking users and want to sort by name. Yea it is very easy
User::where('status_symbol', User::ACTIVE)->orderBy('name')->get()->pluck('full_name', 'id')
full name is attribute from user model
public function getFullNameAttribute() {
return $this->name . ' ' . $this->surname;
}
and results are ok. Now i want send to view (vue) but somehow js sorting again by id as default.
return response()->json(User::where('status_symbol', User::ACTIVE)->orderBy('name')->get()->pluck('full_name', 'id'));
but if i sent without id seems ok, how can make sort by full_name after send response to vue?
axios.get('/api/users/get/all')
.then(response => {
console.log(response.data)
this.reps = response.data
}).catch(e => {
console.error('Failed to load users')
});
Solution 1:[1]
Because you are doing pluck over your results, which give you id => full_name results. Your call should be:
User::where('status_symbol', User::ACTIVE)
->orderBy('name')
->get('name', 'surname', 'id')
->only(['full_name', 'id']);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PunyFlash |
