PHP Articles

Page 12 of 81

How to check if a user email already exists in Laravel?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 5K+ Views

In Laravel, checking if a user email already exists is a common requirement for registration, validation, and duplicate prevention. Laravel provides multiple approaches to accomplish this task efficiently. Before running these examples, ensure you have a Laravel project set up with a User model and database connection configured. Using Laravel Validator The Validator class provides a clean way to check email uniqueness using Laravel's built−in validation rules ?

Read More

How to order the results of related models in Laravel Eloquent?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 526 Views

Laravel Eloquent provides the orderBy() method to sort query results from related models. This method allows you to order data from single tables or joined relationships efficiently. Database Setup Let's work with two related tables − users and user_roles: Users Table id | name | email 1 | Alice | alice@test.com 2 | Bob | bob@test.com 3 | Charlie | charlie@test.com User Roles Table id | user_id ...

Read More

How to update a query on Laravel Fluent using db::raw?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 6K+ Views

You can use DB::raw() when you want to execute arbitrary SQL strings inside Laravel's Fluent query builder. This is particularly useful for complex SQL expressions or when you need to bypass Laravel's query builder methods. Note: To work with DB::raw(), you need to include the DB facade class: use Illuminate\Support\Facades\DB; Using DB::raw() with Update Query You can use DB::raw() within the update() method to pass raw SQL values −

Read More

How to chunk results from a custom query in Laravel?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 7K+ Views

In Laravel, the chunk() method is essential for processing large datasets efficiently. It retrieves data in smaller batches, preventing memory exhaustion when dealing with thousands of records. The method works with both DB facade and Eloquent models. Using DB Facade The chunk() method fetches records in specified batch sizes and processes them within a closure − This example processes 100 records at a time until all records are fetched and displayed. Stopping Chunk Processing You can halt the chunking process by returning false from the closure − ...

Read More

How do Laravel Eloquent Model Attributes map to a table?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 3K+ Views

Laravel Eloquent is an object-relational mapper (ORM) that provides an elegant way to interact with databases. Each table in your database corresponds to an Eloquent model that handles all operations on that table. Model and Table Mapping Convention Laravel follows a specific naming convention for mapping models to tables. If you have a table called users, the model should be named User (singular). Similarly, customers table maps to Customer model. This plural table − singular model pattern is Laravel's default convention, though you can customize it as needed. Database Configuration Before working with models, ensure ...

Read More

How to check database connections in Laravel?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 24K+ Views

Laravel provides several ways to check database connections. The database configuration is stored in config/database.php, and connection details are defined in the .env file. Database Configuration The default database configuration in config/database.php ? 'default' => env('DB_CONNECTION', 'mysql'), Your .env file should contain the database credentials ? DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=test DB_USERNAME=root DB_PASSWORD= Using Controller Method Test database connection within a controller using DB::connection() ?

Read More

How to select count with Laravel's fluent query builder?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 18K+ Views

Laravel's fluent query builder provides an elegant interface for creating and running database queries. It includes built-in protection against SQL injection attacks through PDO parameter binding and supports aggregate methods like count(), min(), max(), avg(), and sum(). To use the fluent query builder, import the DB facade class − use Illuminate\Support\Facades\DB; Let's explore different methods to get count values using Laravel's query builder. We'll use a students table with the following structure and data ? CREATE TABLE students( id ...

Read More

How to get distinct values for non-key column fields in Laravel?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 9K+ Views

In Laravel, you can retrieve distinct values for non-key column fields using several methods. Let's explore different approaches to get unique values from a database table. Assume we have a students table created with the following structure − CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(15) NOT NULL, email ...

Read More

How to select all the column names from a table in Laravel?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 4K+ Views

In Laravel, you can retrieve column names from a database table using several methods. Laravel provides multiple approaches through the Schema facade, Eloquent models, and DB facade to access table structure information. Assume we have created a table named students in MySQL database using the following query − CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(10) NOT ...

Read More

How to find a user in Laravel by Username?

Shilpa Kalangutkar
Shilpa Kalangutkar
Updated on 15-Mar-2026 3K+ Views

In Laravel, there are several ways to find a user by username using Eloquent ORM and the database query builder. Each method provides different levels of functionality and data retrieval options. Using the first() Method The first() method returns the first record that matches the search criteria. It returns null if no matching records are found − The output of the above code is − The name exists in the table Using SELECT Query with Specific Fields You can use the select() method to retrieve only specific fields ...

Read More
Showing 111–120 of 802 articles
« Prev 1 10 11 12 13 14 81 Next »
Advertisements