Eloquent Count Query Laravel 12 With Example

The `count()` method in Laravel 12 counts the number of records in a database table. Basic `count()` Example <?php use App\Models\User; $totalUsers = User::count(); echo “Total Users: ” . $totalUsers; ?> Counts all users in the `users` table. Count with `where()` Condition <?php $activeUsers = User::where(‘status’, ‘active’)->count(); echo “Active Users: ” . $activeUsers; ?> Counts […]

See More

Eloquent whereNotBetween Query Laravel 12 With Example

The `whereNotBetween` method in Laravel 12 Eloquent allows you to filter records where a column’s value does not fall between two specified values. Here’s how to use it with examples: Syntax Model::whereNotBetween(‘column_name’, [$minValue, $maxValue])->get(); Examples Example 1: Excluding Records Within a Date Range <?php // Get users who were NOT registered between Jan 1 and […]

See More

Eloquent whereBetween Query use in Laravel 12

The `whereBetween` method in Laravel 12 Eloquent allows you to query records where a column’s value falls between two values. Here’s how to use it effectively in Laravel 12: Practical Examples 1. Filtering by Date Range <?php // Get orders between two dates $orders = Order::whereBetween(‘created_at’, [ ‘2024-01-01’, ‘2024-12-31’ ])->get(); // Using Carbon for dynamic […]

See More

Laravel 12 Pagination Query Example

Using Laravel 12, you can paginate database records using Eloquent or Query Builder. Laravel provides three pagination methods: 1. `paginate()` → Standard pagination with page numbers. 2. `simplePaginate()` → Uses “Next” and “Previous” links instead of numbered pages. 3. `cursorPaginate()` → Efficient pagination for large datasets using cursors. 1. Eloquent Pagination Example <?php use App\Models\Post; […]

See More

Getting Random Rows in Laravel 12 with Eloquent

Here are several ways to retrieve random rows from your database using Laravel 12’s Eloquent. Method 1: Using `inRandomOrder()` <?php // Get a single random model $randomUser = User::inRandomOrder()->first(); // Get multiple random models (5 in this case) $randomPosts = Post::inRandomOrder()->take(5)->get(); // With conditions $activeProducts = Product::where(‘active’, true) ->inRandomOrder() ->limit(3) ->get(); ?> Method 2: Using […]

See More

Eloquent Limit Query Laravel 12 With Example

Using Laravel 12 you can use Eloquent’s `limit()` and `take()` methods to limit the number of records retrieved from the database. Examples of Using Limit in Laravel 12 Eloquent Using `limit()` Method use App\Models\User; $users = User::limit(5)->get(); // Get only 5 users Using `take()` Method (Alias of `limit()`) $users = User::take(5)->get(); // Get only 5 […]

See More

Eloquent Sort Query Laravel 12 With Example

Laravel 12, you can sort query results using Eloquent’s `orderBy` method. Sorting is a common operation when retrieving data from the database, and Eloquent makes it easy to sort results by one or more columns. Below are examples of how to sort query results using Eloquent in Laravel 12. Basic Sorting with `orderBy` The `orderBy` […]

See More

Laravel 12 Eloquent Delete Query By id Example

In Laravel 12, you can delete a record using Eloquent by its `id` in several ways. Below are different approaches. 1. Using `find()` and `delete()` Method <?php use App\Models\User; $user = User::find(1); // Find user by ID if ($user) { $user->delete(); // Delete the user } ?> 2. Using `destroy()` Method (Multiple IDs Supported) <?php […]

See More

Update Query Use in Laravel 12 Example

In Laravel 12, updating records in the database can be achieved using both the Eloquent ORM and the Query Builder. Here’s how you can perform update operations using each method: 1. Using Eloquent ORM Eloquent provides a fluent and expressive interface for interacting with your database tables. Updating a Record by ID: <?php use App\Models\User; […]

See More

Insert Query Use in Laravel 12 Example

In Laravel 12, you can insert data into the database using Eloquent ORM or the Query Builder. Below are examples of how to perform an insert query in Laravel 12 using both methods. 1. Using Eloquent ORM Eloquent ORM provides a simple and expressive way to insert records into the database. You can create a […]

See More