Laravel 5.6 and 5.7 Basic 1 : Simple CRUD

Laravel is a PHP Open Source framework accomodating Model-View-Controller (MVC) architecture. At the moment (2018) Laravel has reached version 5.6. In this basic tutorial, we will create a simple CRUD. Later we will create a table with only four columns, the id, name, created_at, and updated_at.

Before we begin, laravel should have been installed in your pc environment. Refer to the official website for installation instruction.

Take note that we are using the latest Laravel version 5.6 in this tutorial.

Step 1 – Setting the Database in .env

Once you’ve finished laravel installation, go to .env file and do some modifications. You also need to create your database first. For example, my database name is laravel_indocoder.

Step 2 – Generate Model, Migration File, and Employee Table

We are about to create employee table. This table has 4 field : id, name, created_at, and updated_at. Open terminal or command prompt, go to htdocs folder if you use XAMPP/LAMPP, and type this command  :

Two files will automatically appear :

  1. app\Employee.php
  2. database\migrations\…create_employees_table.php

Open create_employees_table.php and add ‘name’ inside the up() function.

Save the file and do the migration with the following command in your terminal :

If we see our database, four tables will appear :

Membuat CRUD Sederhana Laravel

Step 3 – Create Form View

Next we will create a form to add employee name. Create new file create.blade.php inside resources/views folder.

Step 4 – Create Route and Controller

Next step is to create the route and the controller in order to redirect the page when we enter the url in our browser.

To add a new controller, type this command into your terminal :

The new controller that has just been added will appear inside app\Http\Controllers folder.

Next, open web.php inside routes folder and add the following code on the last line :

Save the file. If you want to see what are the routes that you have for your application, type this command :

Basic Simple Laravel CRUD

Open EmployeeController.php and add the following code inside public function create() :

This code will render your page to create.blade.php.

Type php artisan serve to your terminal, and head over to the following url :

Simple Basic CRUD Laravel 56

Step 5 – Store Data into SQL Database

To save the date from the form into the database, add the following code to EmployeeController.php under public function store() method :

Don’t forget to add the Employee model path under the namespace :

Reopen the previous url and add some name inside the form then Submit :

Basic Laravel 56 Simple CRUD

Open your database, the names will appear in your database :

Step 6 – Create Employee List View

Open EmployeeController.php and add some code to public function index():

Create new file index.blade.php and save it inside resources/views folder :

Basic Laravel 56 Simple Crud

Step 7 – Edit Employee

To update the employee, open EmployeeController.php and add the following code :

And create a new file edit.blade.php inside resources/views folder :

Next, modify public function update() inside EmployeeController.php

Click edit on your Employee List and then click Update :

Laravel 56 Simple CRUD Example

Step 8 – Delete Employee

To delete an employee, modify public function destroy() inside EmployeeController.php

Laravel 56 Basic Simple CRUD Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.