1

I have the following query. I want to get data from the database sorted alphabetically:

$states = State::where('status', 1)->sortBy('name')->get();
1

2 Answers 2

2

Which framework are you using If laravel then write below syntax

$states = State::where('status', 1)->orderBy('name', 'ASC')->get();
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.