Laravel

What is the difference between find(), findOrFail(), first(), firstOrFail(), get(), list(), toArray()

Kartic1 min read

1. find($id) takes an id and returns a single model. If no matching model exist, it returns null.

2. findOrFail($id) takes an id and returns a single model. If no matching model exist, it throws an error1.

3. first() returns the first record found in the database. If no matching model exist, it returns null.

4. firstOrFail() returns the first record found in the database. If no matching model exist, it throws an error1.

5. get() returns a collection of models matching the query.

6. pluck($column) returns a collection of just the values in the given column. In previous versions of Laravel this method was called lists.

7. toArray() converts the model/collection into a simple PHP array.

Share this articleLinkedIn X

Discussion (0)

Sign in to join the discussion.

Delete this comment?

This will also remove its replies.

Keep exploring

Laravel2 min read

What are accessors and mutators in laravel?

Accessors are a way to retrieve data from eloquent after doing some operation on the retrieved fields from the database. For example, if we need to combine the first and last names...

Kartic · Dec 22, 2022
Laravel1 min read

What is Eloquent in Laravel?

Eloquent is the ORM used to interact with the database using Model classes. It gives handy methods on class objects to make a query on the database. It can directly be used to ret...

Kartic · Dec 22, 2022
Laravel1 min read

Explain Closures in laravel?

In Laravel, a closure is an anonymous function that often used as callback methods. Developers can also use it as a parameter in a function. Example: In Laravel, a closure is an an...

Kartic · Oct 24, 2019

Follow the engineering notes.

Subscribe for technical articles from iTechOrbit.