Create Read Update Delete (CRUD) procedures in Laravel 5.2

In this post, we will know the most useful method laravel 5.2. Here we will create a simple Create Read Update Delete (CRUD) application with Laravel 5.2. Following steps help to beginners for implement Create Read Update Delete (CRUD) procedures in Laravel 5.2.

CRUD Operations contains,
  • Add a new Form and insert data into the database.
  • Get whole data from the database and show on the view page.
  • Get and modify existing details and update new data into the database with the help of a primary key and required condition keys.
  • Delete data from the database with the help of a primary key or condition keys.
Step 1: Basic Setup

Download the Laravel package and set the basic application requirements like DB connection setting in .env file. Then open the command prompt point to project folder path.

Step 2: Create Model & Migration classes

Create a Product model class that interacts with “products” table.

Here -m parameter use to create a database migration file at the time of model class creation. Created model class file present in app/ folder. In that file, we need to add some basic details like $fillable, $table and $primaryid for the particular table.

Step 3: Created migrations class file present in database/migrations folder. Open the created migrations file and add the required column names to store the basic data from the form with primary key and timestamps. The migrations file name start with some number and table name format.

Step 4: Controller Creation
The controller is used to handling user request with one class. To handle all the form related request, define a Product Controller by creating a ProductController.php file. This file present in app/Http/Controllers/ folder.

At the starting of the file, we need to use a particular model class to store form data into defined table. It contain index(), show(), add(), insert(), edit(), update(), delete() methods.

Step 5: Route defining 

To create the CRUD procedures, we need to set routes, routes are used to handle the view and form submission request process with a controller. A route tells which view want to show to the requested URLs with type. Here get() and post() methods are used define various routes. get() method for showing details from database or controller to view page. post() method for handling form submission data requests to a database. All routes are defined in routes.php it present app/Http/ folder.

Step 6: Preparing page Layout

Each and every application have a basic layout for entire pages. Here we need to define our app layout with some basic details. Layouts are present in resources/views/layouts/ folder. Here we use basic pageLayout.blade.php as our layout.

Step 7: Create Page Views for Form data

All view pages are placed into resources/views/ folder. In that, we need to create one folder with our CRUD handling name like a product, category, state, city etc. Here we create product folder for our product form view pages. Product folder contains index.blade.php, add.blade.php, show.blade.php, edit.blade.php files.

Step 8: Show the All Product List

In this section, all product details list are shown to frontend user for manage the details like Add, View, Update or Remove from Database. Create a file named as index.blade.php as save the data with the following code

Step 9: Create Add new product details

In this section, we need to create New product details form with post data route and save it as add.blade.php

Step 10: View the Added Product Details

In this section, we going to show the created or added product details from a database into HTML view page. Following code to use show the product details, add into the show.blade.php

Step 11: Edit or Update existing details

In this section, we can update or edit the existing product details with a help of primary key. Following code to use fetch the product details and update it and submit the form. It will update the details in a database. Use the below code and save the file name as edit.blade.php

Step 12: Running our application

Everything it’s done Now we can run the Product CRUD procedures with corresponding URLs defined in routes.

example.com/products    –> Show the index page
example.com/products/add    –> Add new product details
example.com/products/show/{product_id}    –> View or read the particular product details
example.com/products/edit/{product_id}    –> Fetch the product details for edit and update in DB

 

Learn Infinity

Learn Infinity is the most famous Programming & Web Development blog. Our principal is to provide the best online tutorial on web development. We execute the best tutorials for web experts this will help developers, programmers, freelancers and proving free resource you can download or preview the tutorials.

Leave a Reply

Your email address will not be published.

Back to top