The Query Builder in Laravel provides a fluent interface for building and executing database queries. It offers an alternative to raw SQL and Eloquent ORM, making it ideal for complex or custom queries.
The Query Builder has been a part of Laravel since its inception, designed to simplify interacting with databases by providing an intuitive API.
count
and sum
for efficient data retrieval.Retrieve data using the Query Builder:
use Illuminate\Support\Facades\DB;
$users = DB::table('users')
->where('status', 'active')
->orderBy('created_at', 'desc')
->get();
Insert data:
DB::table('users')->insert([
'name' => 'John Doe',
'email' => '[email protected]',
'status' => 'active',
]);
The Query Builder provides a powerful yet simple way to interact with your database, balancing control and ease of use.