Shilpa Kalangutkar

Shilpa Kalangutkar

45 Articles Published

Articles by Shilpa Kalangutkar

Page 4 of 5

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

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
Showing 31–40 of 45 articles
Advertisements