0

What is the best way to retrieve a record based on a unique value?

I know I can, for example, use: $person = Person::where('fullname', "John Johnson")->first()

Since the fullname only contains unique values, is there a more efficient way similar to when you are using Person::find($id)?

2
  • Why is the fullname unique? There might be persons with the same fullname.. Commented Aug 11, 2015 at 12:21
  • Yeah, not the greatest example. Commented Aug 11, 2015 at 15:31

1 Answer 1

2

There shouldn't be any efficiency difference in terms of the query that is being generated - both queries will generate a very similar SQL:

SELECT * FROM persons WHERE id = 5;
SELECT * FROM persons WHERE fullname = 'John Johnson' LIMIT 1;

There can be difference in efficiency that depends on your schema configuration, as primary keys usually have index created automatically and for other columns you need to create the index yourself.

Sign up to request clarification or add additional context in comments.

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.