Found 1060 Articles for PHP

How to decode JSON objects in Laravel and apply for each loop on that?

Shilpa Kalangutkar
Updated on 30-Aug-2022 13:03:26

11K+ Views

The json_decode() method is a PHP built-in function, which helps to convert a JSON object into a PHP object. It takes the input value as a string and returns a readable PHP object Example 1 Following is an example of the json_decode() method − $studentobj = '{"Neha":35, "Arbaaz":37, "Rehan":43}'; print_r(json_decode($studentobj)); Output The output of the above code is − stdClass Object ( [Neha] => 35 [Arbaaz] => 37 [Rehan] => 43 ) Let us now loop through the final object that we get from json_decode(). Example 2 The JSON object is ... Read More

How to create and update Laravel Eloquent?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:58:27

16K+ Views

Assume we have created a table named students using the CREATE statement as shown below − CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(15) NOT NULL, email VARCHAR(20) NOT NULL, created_at VARCHAR(27), updated_at VARCHAR(27), address ... Read More

How to extract raw form data in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:55:36

7K+ Views

To demonstrate the above, we are going to make use of the below form It has username and password fields. Let us try to get raw form data in the methods shown below −Example 1 Using file_get_contents() file_get_contents() method is a built-in PHP function and it returns the file content into a string format. Now to get raw data of the form you can give the input to the file_get_contents as php://input

How to get a list of registered route paths in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:46:40

4K+ Views

All the routes are stored inside the routes/ folder. If you open routes/web.php you will see the list of routes defined for your application. If you want to work with routes, you need to include the following class − use Illuminate\Support\Facades\Route; The routes/web.php file along with above class is added by default when laravel is installed. Here is a basic route that gets called when you hit the URL http://localhost:8000/. It is called the default route. The view(‘test’) is called when the URL is hit. use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('test'); }); Let us ... Read More

How to delete a file from the public folder in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:39:14

5K+ Views

You can make use of the File facade class. To work with File facade you need to include the class as shown below − use Illuminate\Support\Facades\File; Here is a list of examples that shows how to delete the files from the public folder. The files details in the public folder are as follows –  Example 1 To delete files using delete() method from File façade. The delete() method will delete the files given. It can take a single file or an array of files as an input. You can delete a single file from the public folder as ... Read More

How to pass an array as a URL parameter in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:33:20

12K+ Views

To pass an array as URL parameter you can make use of php built-in function http_build_query(). The http_build_query() returns you an URL-encoded query string. Example 1 Using http_build_query() Following is an example of this method − $data = array( 'field1' => 'test', 'field2' => 'xyz' ); echo http_build_query($data) . ""; Output The output of the above code is − field1=test&field2=xyz The following example shows how to make use of http_build_query() when you have an array and the same needs to pass as a URL parameter.

How to get the last inserted ID using Laravel Eloquent?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:31:46

17K+ Views

Eloquent is a new object relational mapper (ORM) that helps to interact with database. With Eloquent each table has a mapping Model that takes care of all the operations on that table. Model in laravel represents the table in the database. For example if you have table customers, the model name will be customer, for users it will be user, employees it will be employee. The table name has to be plural and the model name has to be singular. This is a pattern followed, but that does not stop you from using the naming convention of your choice for ... Read More

How to select certain fields in Laravel Eloquent?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:29:55

8K+ Views

Laravel Eloquent provides many ways to select the fields that you need from the table. Here are a few examples that show how it can be done. Example 1 The following is an example of this −

How to get the Current URL inside the @if Statement in Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:26:02

1K+ Views

To get the current URL you can use the methods explained in the example below − Example 1

How would you insert a new user into a MySQL table from within Laravel?

Shilpa Kalangutkar
Updated on 30-Aug-2022 12:22:06

917 Views

You can insert a new user in the following ways as shown in the examples below. To insert a new user from Laravel you can do it using artisan seeder. To do that first create a seeder using the command below − php artisan make:seeder UserSeeder Once the command is executed, you will get the UserSeeder.php file inside database/seeders. Add the code to add user records inside users table in the seeder file as shown below. Example 1

Advertisements