Website Programming CRUD PHP Framework Laravel – Part 1

Tram Ho

Instructions for Laravel 5.5 CRUD. Practice CRUD in the PHP Framework Laravel. Create Website CRUD User project with Laravel PHP Framework 5.5. Practice CRUD.

This article will guide you step by step to create a Laravel 5.5 CRUD project to manage users. User management will include:

  • Create – Add a new user.
  • Read – Read user information
  • Update – Update or edit the user information.
  • Delete – Delete user data.

Sample project: https://namcoi.com/du-an/aptech-php-laravel-crud-user/public/users

Source code: https://github.com/namnh06/aptech-php-laravel-crud-user

Step 1: Create a Laravel 5.5 project using Composer Create-Project

Use Terminal to install and initialize the Website Laravel CRUD 5.5 project with the following command:

The above command will install and initialize a new Laravel project named user-crud-laravel with PHP Framework Laravel 5.5.

Step 2: Working with Database and .env file

Create a new Database named user_crud_laravel at localhost via phpMyAdmin or Workbench, using the following command:

Set up the information in the .env file of the newly created Laravel Project-crud-laravel:

Note that XAMPP’s default DB_USERNAME is root and the password is left blank.

Step 3: Use Migration to create users data table

Create file Migration through php artisan to create users data table:

Use the flag for the –create = users command to make sure Laravel will support us with the command in the file migration. Regardless of whether your statement encountered a syntax error when declaring the file name as create_users table or not.

Make sure that in user-crud-laravel> database> migrations directory there is only one migration file, the existing files can be deleted. Edit the newly created migration file as follows:

In the above commands, we have added 3 data columns: name, email, password. Corresponds to the username data column, the user’s email and the user’s password in the users data table.

Execute the command below to let the migration file “run” and create the users data table:

Through phpMyAdmin or Workbench to check if users data table is available or not?

Step 4: Use Seeder in Laravel to add data for testing purposes

Create the file seed data through the command:

The above command will create the UsersTableSeeder.php file in the user-crud-laravel folder> database> seeds.

Edit the UsersTableSeeder.php file with the following code:

In function run, we create the object from Class Faker Factory. PHP Library Faker is built into Laravel 5.5. Using PHP’s loop and Laravel’s Query Builder add 5 users to the users table.

Execute the UsersTableSeeeder.php file using the following command:

Check in the Database user_crud_laravel and users data tables to make sure the data has been added. We already have a project using PHP Framework Laravel 5.5 with Migration and Seeder, which is not yet the CRUD features that a Website can bring. Migration only makes creating data tables easier and more manageable. Seeder supports adding dummy data for testing purposes.

The next section will introduce Routes, Controller, Model and View in Laravel 5.5.

Share the news now

Source : Viblo