Laravel Articles

Page 4 of 5

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

How to alias a table in Laravel Eloquent queries using Query Builder?

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

Laravel Eloquent provides several ways to alias tables in database queries, making your code more readable and enabling complex joins. Table aliases are particularly useful when working with multiple tables or when you need to shorten long table names. Sample Data Let's assume we have a students table with the following data − +----+---------------+------------------+-----------------------------+-----------------------------+---------+------+ | id | name | email | created_at ...

Read More

How to access the public directory in Laravel?

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

In Laravel, you can access files in the public directory using the File facade or PHP's built−in functions. The public directory is where Laravel stores assets like images, CSS, and JavaScript files that need to be directly accessible via web URLs. Make sure to import the File facade in your controller: use Illuminate\Support\Facades\File; Laravel Public Directory Structure public/ images/ css/ ...

Read More

How to Use OrderBy for Multiple Columns in Laravel?

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

In Laravel, you can sort query results by multiple columns using the orderBy() method chained together. This allows you to create complex sorting logic where records are first sorted by one column, then by another column for records with identical values in the first column. Syntax The basic syntax for ordering by multiple columns in Laravel is − Model::orderBy('column1', 'ASC/DESC') ->orderBy('column2', 'ASC/DESC') ->get(); Note: To run the examples below, ensure you have Laravel installed with a configured database connection and a Student model ...

Read More

What is Fillable Attribute in a Laravel model?

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

The fillable attribute in Laravel models is a security feature that defines which database fields can be safely mass−assigned. It protects against mass assignment vulnerabilities by explicitly specifying which fields can be updated through methods like create() or update(). What is Mass Assignment? Mass assignment occurs when you pass an array of data directly to model methods. Without proper protection, malicious users could potentially modify unintended fields by adding extra parameters to HTTP requests. Setting Up Fillable Attributes First, create a model using Artisan command ? Run the following command to create a Student ...

Read More

How to validate an array in Laravel?

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

In Laravel, you can validate array data using the built−in Validator class. This is essential for ensuring data integrity before processing user input or form submissions. To use Laravel's validation features, ensure you have Laravel installed and include the Validator facade in your controller. use Illuminate\Support\Facades\Validator; Validating Associative Arrays When working with associative arrays, you define validation rules for each key. Here's how to validate an array with firstname, lastname, and address fields ? Array Validation is Successful Missing Required Fields If a ...

Read More

How to add a new column to an existing table of Laravel in a migration?

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

Laravel migrations provide a convenient way to modify your database schema over time. The make:migration Artisan command enables you to add new columns to existing tables without affecting existing data. Creating an Initial Table First, let's create a basic table using the migration command. The syntax for creating a new table is ? php artisan make:migration create_students_table This generates a migration file in the database/migrations directory ?

Read More
Showing 31–40 of 45 articles
Advertisements