I have the following query. I want to get data from the database sorted alphabetically:
$states = State::where('status', 1)->sortBy('name')->get();
I have the following query. I want to get data from the database sorted alphabetically:
$states = State::where('status', 1)->sortBy('name')->get();
You need to use order by SQL keywords to get it alphabetically ordered data as ascending or descending order. For laravel use check Ordering, Grouping, Limit, & Offset from laravel docs.
For your use case it is simple as
$states = State::where('status', 1)->orderBy('name', 'ASC')->get();
for ascending order