Table of contents

Laravel Eloquent Query

In this post, I will share an example for Laravel Eloquent Query. The Laravel Eloquent all() method will retrieve all results in the model's table. But since each Eloquent model serves as a query builder, you can add additional constraints to your queries and invoke the method get() to retrieve the results.

When doing a simple query to get all records from a table using MySQL your statement:

SELECT * FROM posts

 

But if we use an eloquent query then we will just use all() methods:

$posts = Post::all();

 

Applying query builder methods to our Laravel eloquent query:

$posts = Post::where('status', 1)
               ->orderBy('title')
               ->take(10)
               ->get();

 

That's it, now you have the basics on how to use Laravel 8 eloquent queries. I hope it helps.

 

For more methods about query builder just visit the full documentation.