Laravel 6 CRUD Application Tutorial With Example

In this tutorial you will learn about the Laravel 6 CRUD Application Tutorial With Example and its application with practical example.

Laravel 6 CRUD Application Tutorial With Example

In this Laravel 6 CRUD Example Tutorial, you will learn to create a simple CRUD (Create, Read, Update and Delete) application using Laravel 6. In this step by step guide, we will be creating a simple blog application with post table and implement CRUD operations.

Install Laravel 6

First of all we need to create a fresh laravel project, download and install Laravel 6 using the below command

Make sure you have composer installed.

Now, lets switch to the project directory and start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/

Output:-

laravel-5-7-crud-tutorial-with-example-2

Configure Database In .env file

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Create Database Table

Now, we have to define table schema for posts table. Open terminal and use the following artisan command to generate <timestamp>create_posts_table.php migration.

Once this command is executed you will find a migration file created under “database/migrations”

laravel-5-7-crud-tutorial-with-example-4

lets open migration file and put following code in it –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the posts table will be created in database along with migrations, password_resets and users table.

Create Resource Controller and Model

In this step, we will use the following command to create post resource controller and model together

Once the above command executed, it will create a resource controller file “PostController.php” in “app/Http/Controllers/” directory. Resource controller automatically creates a list of following methods together with their parameters –

Index()
Create()
Store()
Show()
Edit()
Update()
Destroy()

app/Http/Controllers/PostController.php

Above command also creates model file “Post.php” in app directory

app/Post.php

Next, we have to assign fillable fields using fillable property inside Post.php file. Update app/Post.php with following code –

Create Resource Route

After this, we need to add resource route for post controller. Lets open “routes/web.php” file and add following route.

routes/web.php

we can use the following artisan command to list all the routes –

Output:-

Create Blade Files

In this step, we will create application view files for the first create following two directories for blade files inside “resources/views/” directory –

  • layouts
  • posts

Next, create a main layout file “app.blade.php” in “resources/views/layouts/” directory and put the following code in it.

resources/views/layouts/app.blade.php

Now, we will create following blade files in “resources/views/posts” directory for CRUD (Create, Retrieve, Update, Delete) operations.

1) create.blade.php

2) edit.blade.php

3) index.blade.php

4) show.blade.php

Create Operation

In this step, first we will create a view file named create.blade.php inside “resources/views/posts” directory and put the following code inside it –

resources/views/posts/create.blade.php

Now open the PostController.php file, and modify the create function as following –

Once we have created the blade file for create operation and modified the create method in PostController.php, lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/posts/create

Output:-

laravel-6-crud-application-tutorial-with-example-1-1

Validate and Save Data

After this, we create a script to validate and save data in database upon form submission. Lets open the PostController.php file, and modify the store function as following –

Restart the development server using following artisan command –

Now, visit the following URL in browser to create a post –

http://localhost:8000/posts/create

Here, now if you press the submit button the without filling up the required data(title or body), the store method validates the form data and you will be not able to save data to database and notified with following errors –

laravel-6-crud-application-tutorial-with-example-2

And, if after filling up the post title and body you press the submit button the form data will be submitted to the store method in PostController.php. The store method validates the form data and insert into the post table –

Upon the successful submission we will redirected to the index method –

http://localhost:8000/posts/

Retrieve Data

In this step, we will create a two view file named index.blade.php and show.blade.php inside “resources/views/posts” directory two list all the posts created and to view them individually. Lets create the blade files and put the following code inside it –

resources/views/posts/index.blade.php

resources/views/posts/show.blade.php

Now open the PostController.php file, and modify the index and show function with following code –

index method:-

show method:-

Restart the development server again, now you will be able to list all the posts and to view them individually.

Update Data

In the previous step, we already have a “Edit” button linked to edit action for each of the post respectively.In this step, we will create a view file named edit.blade.php inside “resources/views/posts” directory and put the following code inside it –

resources/views/posts/edit.blade.php

Now open the PostController.php file, and modify the edit and update function with following code –

edit() Method:-

update() method:-

The edit() renders the edit view for respective post with prefilled post data, upon the form submission the form data will be submitted to the update() method in PostController.php. The update() method validates the form data and update the post data, after the successful update operation we will redirected to the index method.

Delete Operation

For deleting the post, we already have a “Delete” action button linked to delete action for each of the post respectively.In this step there is no need to create a view file, we just have to open the PostController.php file, and modify the destroy() function with following code –

Now, when we click to the “Delete” action button, the respective post will be deleted.

Finally, the PostController.php file looks like this –

app/Http/Controllers/PostController.php

Now we have a working Laravel 6 CRUD Application ready, restart the development server using following artisan command –

and visit the following URL in browser to see the complete Laravel 6 CRUD Application in action –

http://localhost:8000/posts/

laravel-6-crud-application-tutorial-with-example-3

In this tutorial we have learn about the Laravel 6 CRUD Application Tutorial With Example and its application with practical example. I hope you will like this tutorial.