Found 1046 Articles for PHP

How to access the public directory in Laravel?

Shilpa Kalangutkar
Updated on 29-Aug-2022 11:59:49

6K+ Views

To get the file list from a public directory you can make use of the File facade class. To work with it you need to include the File class using the following statement – use Illuminate\Support\Facades\File; Assume we have the following files in the public folder − Example 1 Following Program tries to retrieve all the images from the public_folder −

How to Use OrderBy for Multiple Columns in Laravel?

Shilpa Kalangutkar
Updated on 29-Aug-2022 12:08:07

5K+ Views

The ORDERBY clause is used to arrange the columns in a table in ascending or descending order. By default it sorts the columns in ascending order, if you need to sort in descending order you should use DSC along with the clause. Syntax Following is the syntax of this statement − SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC; Assume we have created a table named Students in MySQL database using the following query − CREATE TABLE students( id INTEGER ... Read More

What is Fillable Attribute in a Laravel model?

Shilpa Kalangutkar
Updated on 29-Aug-2022 13:04:41

24K+ Views

The fillable property is used inside the model. It takes care of defining which fields are to be considered when the user will insert or update data. Only the fields marked as fillable are used in the mass assignment. This is done to avoid mass assignment data attacks when the user sends data from the HTTP request. So the data is matched with the fillable attributes before it is inserted into the table. To understand fillable attributes let us create a model as shown below php artisan make:model Student C:\xampp\htdocs\laraveltest>php artisan make:model Student Model created successfully. C:\xampp\htdocs\laraveltest> Now let ... Read More

How to validate an array in Laravel?

Shilpa Kalangutkar
Updated on 29-Aug-2022 11:43:24

6K+ Views

There are various ways to validate the input data in Laravel. One of those is the Validator class. The first step is that you need to include this class as shown below − use Illuminate\Support\Facades\Validator; When you work with an array you need to mention the keys and the rules to be used in the array. For example, consider the following array. Here, we are adding the keys, firstname, lastname, and address along with the values. Using rules we define all these elements are mandatory and of the data type String. $formData = array( 'firstname' => ... Read More

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

Shilpa Kalangutkar
Updated on 29-Aug-2022 11:37:31

12K+ Views

The make:migration Artisan in Laravel (9) enables you to generate a database migration. You can find the resultant migration files are the database/migrations directory. Using this command, we can add a new column to a table (in addition to other operations). Syntax First of all let us first create a table using the make:migration command. Following is the syntax to create a new table in Laravel using this command − php artisan make:migration create_yourtablename_table Here, the table name is your_table_name. So let us create a table with the name: students. The command to create table students is as follows ... Read More

How to access images inside a public folder in Laravel?

Shilpa Kalangutkar
Updated on 14-Sep-2023 13:33:08

28K+ 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 folder as shown below Now let us use the image to view inside the browser. Using url() method The url() helper function helps to return you the full path i.e., it will add the HTTP or HTTPS and return the complete path for the given URL. For example, consider the following URL ... Read More

How to use Validations in Laravel?

Shilpa Kalangutkar
Updated on 29-Aug-2022 09:24:48

309 Views

It is important to validate the data that comes from the HTML forms. Laravel provides you with the validate() method using which you can define the rules on the input fields the way you want. The following examples show the working of validate() and how to make use of it on the HTML form input fields. To test validation, create controller as shown below − php artisan make:controller testvalidationController You will get the following output once it is done − C:\xampp\htdocs\laraveltest>php artisan make:controller testvalidationController Controller created successfully. C:\xampp\htdocs\laraveltest> Open the controller “testvalidationController” and add validation rules for the ... Read More

How to get the server IP with Laravel?

Shilpa Kalangutkar
Updated on 29-Aug-2022 09:00:15

6K+ Views

The server IP will be the IP address of the server you are connected to. When you are working with Laravel to get the server IP you can make use of $_SERVER['SERVER_ADDR']. The $_SERVER variable acts as a global variable in PHP. It holds details like header information, script location, and other miscellaneous details. Example 1 The following example retrieves the details of the $_SERVER global variable.

How to check if a Laravel collection is empty?

Shilpa Kalangutkar
Updated on 29-Aug-2022 08:52:14

17K+ Views

Before we answer the above question, let us first understand what collections are in Laravel. Collection in Laravel is an API wrapper that helps you deal with different operations to be performed on arrays. It makes use of the class Illuminate\Support\Collection to deal with arrays in Laravel. To create a collection from a given array you need to make use of the collect() helper method that returns a collection instance. Later you can use a chain of methods like converting to lowercase and sorting on the collection instance. Example  The example in this section demonstrates how to create a collection from ... Read More

Difference between PHP and C

Pradeep Kumar
Updated on 10-Aug-2022 07:16:55

1K+ Views

Many developers will indeed agree that comparing C Programming and PHP is unfair because they differ in web development. PHP is by far the most famous serverside scripting language. JavaScript works with things on the client end without ever returning to the server, while PHP manages things on the application server. PHP is based on the C programming language, thus everyone with a basic understanding of C will find it easy to learn PHP. What is PHP? PHP is a general−purpose programming language that is primarily utilized in the development of websites. It was developed in 1994 by DanishCanadian programmer ... Read More

Advertisements