PHP Articles

Page 13 of 81

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 6K+ 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

How to access images inside a public folder in Laravel?

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

If you have an image stored in the public folder of Laravel, you can access or display it in the browser using various methods. In this article, we will learn some of these methods. Assume we have the image of a flower (flower.jpg) stored in the public/images folder as shown below ? Laravel Project Structure public/ images/ flower.jpg Now let us use the image to view inside the browser. Using url() Method The ...

Read More

How to get the server IP with Laravel?

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

In Laravel, you can get the server IP address using the global $_SERVER['SERVER_ADDR'] variable or Laravel's built−in request methods. The server IP represents the IP address of the server hosting your application. The $_SERVER superglobal variable contains information about headers, paths, and script locations, including server details. Using $_SERVER['SERVER_ADDR'] The simplest method is to access the server IP directly from the $_SERVER superglobal ? 127.0.0.1 Using Laravel's Request Object Laravel provides the request() helper function with a server() method to access server variables ? ...

Read More

How to check if a Laravel collection is empty?

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

Before checking if a Laravel collection is empty, let's understand what collections are in Laravel. Collection in Laravel is an API wrapper that helps you deal with different operations on arrays. It uses the class Illuminate\Support\Collection to handle arrays in Laravel. To create a collection from an array, use the collect() helper method that returns a collection instance. You can then chain methods like converting to lowercase and sorting on the collection. Installation: Make sure you have Laravel installed and create a controller using: php artisan make:controller UserController Using the isEmpty() Method The isEmpty() ...

Read More

Difference between PHP and C

Pradeep Kumar
Pradeep Kumar
Updated on 15-Mar-2026 2K+ Views

PHP and C are two distinct programming languages that serve different purposes. PHP is a server−side scripting language primarily used for web development, while C is a low−level procedural programming language used for system programming. Understanding their differences helps developers choose the right tool for their projects. What is PHP? PHP (Hypertext Preprocessor) is a general−purpose programming language developed by Rasmus Lerdorf in 1994. It is primarily utilized for web development and runs on web servers to generate dynamic content. PHP code is processed by a PHP interpreter installed on web servers as a module, daemon, ...

Read More
Showing 121–130 of 802 articles
« Prev 1 11 12 13 14 15 81 Next »
Advertisements