Laravel 6 CRUD Application Tutorial

Laravel 6 CRUD Application Tutorial

In this tutorial, you will learn very basic CRUD operation with Laravel new version 6. I am going to show you step by step from scratch so, I will better to understand if you are new in Laravel.

Insert Update Delete module is primary requirement for each project, so in this tutorial i will give you step by step instruction for creating crud (Create Read Update Delete) Application in Laravel 6. you will understand how to use resource route, controller, blade files, model and migration for crud operation in Laravel 6.

Laravel 6 is just released by tomorrow, Laravel 6 gives several new features and LTS support. So if you are new to laravel then this tutorial will help you create insert update delete application in Laravel 6.

You just need to follow few step and you will get basic crud stuff using controller, model, route, bootstrap 4 and blade..

In this tutorial, you will learn very basic crud operation with Laravel new version 6. I am going to show you step by step from scratch so, i will better to understand if you are new in Laravel.

Step 1 : Install Laravel 6

first of all we need to get fresh Laravel 6 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Database Configuration

In second step, we will make database configuration for example database name, username, password etc for our crud application of laravel 6. So let's open .env file and fill all details like as bellow:

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name(blog)
DB_USERNAME=here database username(root)
DB_PASSWORD=here database password(root)
Step 3: Create Migration

we are going to create crud application for product. so we have to create migration for "products" table using Laravel 6 php artisan command, so first fire bellow command:

php artisan make:migration create_products_table --create=products

After this command you will find one file in following path "database/migrations" and you have to put bellow code in your migration file for create products table.

<?php

use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->text('detail'); $table->timestamps(); }); }

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('products');
}

}

Now you have to run this migration by following command:

php artisan migrate
Step 4: Add Resource Route

Here, we need to add resource route for product crud application. so open your "routes/web.php" file and add following route.

routes/web.php

Route::resource('products','ProductController');
Step 5: Add Controller and Model

In this step, now we should create new controller as ProductController. So run bellow command and create new controller. bellow controller for create resource controller.

php artisan make:controller ProductController --resource --model=Product

After bellow command you will find new file in this path "app/Http/Controllers/ProductController.php".

In this controller will create seven methods by default as bellow methods:

1) index()

2) create()

3) store()

4) show()

5) edit()

6) update()

7) destroy()

So, let's copy bellow code and put on ProductController.php file.

app/Http/Controllers/ProductController.php

<?php

namespace App\Http\Controllers;

use App\Product; use Illuminate\Http\Request;

class ProductController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $products = Product::latest()->paginate(5);

    return view('products.index',compact('products'))
        -&gt;with('i', (request()-&gt;input('page', 1) - 1) * 5);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('products.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $request-&gt;validate([
        'name' =&gt; 'required',
        'detail' =&gt; 'required',
    ]);

    Product::create($request-&gt;all());

    return redirect()-&gt;route('products.index')
                    -&gt;with('success','Product created successfully.');
}

/**
 * Display the specified resource.
 *
 * @param  \App\Product  $product
 * @return \Illuminate\Http\Response
 */
public function show(Product $product)
{
    return view('products.show',compact('product'));
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  \App\Product  $product
 * @return \Illuminate\Http\Response
 */
public function edit(Product $product)
{
    return view('products.edit',compact('product'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \App\Product  $product
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, Product $product)
{
    $request-&gt;validate([
        'name' =&gt; 'required',
        'detail' =&gt; 'required',
    ]);

    $product-&gt;update($request-&gt;all());

    return redirect()-&gt;route('products.index')
                    -&gt;with('success','Product updated successfully');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  \App\Product  $product
 * @return \Illuminate\Http\Response
 */
public function destroy(Product $product)
{
    $product-&gt;delete();

    return redirect()-&gt;route('products.index')
                    -&gt;with('success','Product deleted successfully');
}

}

Ok, so after run bellow command you will find "app/Product.php" and put bellow content in Product.php file:

app/Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model { protected $fillable = [ 'name', 'detail' ]; }

Step 6: Add Blade Files

In last step. In this step we have to create just blade files. So mainly we have to create layout file and then create new folder "products" then create blade files of crud app. So finally you have to create following bellow blade file:

1) layout.blade.php

2) index.blade.php

3) create.blade.php

4) edit.blade.php

5) show.blade.php

So let's just create following file and put bellow code.

resources/views/products/layout.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 6 CRUD Application - ItSolutionStuff.com</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>

<div class="container"> @yield('content') </div>

</body> </html>

resources/views/products/index.blade.php

@extends('products.layout')

@section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Laravel 6 CRUD Example from scratch - ItSolutionStuff.com</h2> </div> <div class="pull-right"> <a class="btn btn-success" href="{{ route('products.create') }}"> Create New Product</a> </div> </div> </div>

@if ($message = Session::get('success'))
    &lt;div class="alert alert-success"&gt;
        &lt;p&gt;{{ $message }}&lt;/p&gt;
    &lt;/div&gt;
@endif

&lt;table class="table table-bordered"&gt;
    &lt;tr&gt;
        &lt;th&gt;No&lt;/th&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Details&lt;/th&gt;
        &lt;th width="280px"&gt;Action&lt;/th&gt;
    &lt;/tr&gt;
    @foreach ($products as $product)
    &lt;tr&gt;
        &lt;td&gt;{{ ++$i }}&lt;/td&gt;
        &lt;td&gt;{{ $product-&gt;name }}&lt;/td&gt;
        &lt;td&gt;{{ $product-&gt;detail }}&lt;/td&gt;
        &lt;td&gt;
            &lt;form action="{{ route('products.destroy',$product-&gt;id) }}" method="POST"&gt;

                &lt;a class="btn btn-info" href="{{ route('products.show',$product-&gt;id) }}"&gt;Show&lt;/a&gt;

                &lt;a class="btn btn-primary" href="{{ route('products.edit',$product-&gt;id) }}"&gt;Edit&lt;/a&gt;

                @csrf
                @method('DELETE')

                &lt;button type="submit" class="btn btn-danger"&gt;Delete&lt;/button&gt;
            &lt;/form&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    @endforeach
&lt;/table&gt;

{!! $products-&gt;links() !!}

@endsection

resources/views/products/create.blade.php

@extends('products.layout')

@section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Add New Product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> </div> </div> </div>

@if ($errors->any()) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif

<form action="{{ route('products.store') }}" method="POST"> @csrf

 &lt;div class="row"&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
        &lt;div class="form-group"&gt;
            &lt;strong&gt;Name:&lt;/strong&gt;
            &lt;input type="text" name="name" class="form-control" placeholder="Name"&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
        &lt;div class="form-group"&gt;
            &lt;strong&gt;Detail:&lt;/strong&gt;
            &lt;textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"&gt;&lt;/textarea&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12 text-center"&gt;
            &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
&lt;/div&gt;

</form> @endsection

resources/views/products/edit.blade.php

@extends('products.layout')

@section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit Product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> </div> </div> </div>

@if ($errors-&gt;any())
    &lt;div class="alert alert-danger"&gt;
        &lt;strong&gt;Whoops!&lt;/strong&gt; There were some problems with your input.&lt;br&gt;&lt;br&gt;
        &lt;ul&gt;
            @foreach ($errors-&gt;all() as $error)
                &lt;li&gt;{{ $error }}&lt;/li&gt;
            @endforeach
        &lt;/ul&gt;
    &lt;/div&gt;
@endif

&lt;form action="{{ route('products.update',$product-&gt;id) }}" method="POST"&gt;
    @csrf
    @method('PUT')

     &lt;div class="row"&gt;
        &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
            &lt;div class="form-group"&gt;
                &lt;strong&gt;Name:&lt;/strong&gt;
                &lt;input type="text" name="name" value="{{ $product-&gt;name }}" class="form-control" placeholder="Name"&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
            &lt;div class="form-group"&gt;
                &lt;strong&gt;Detail:&lt;/strong&gt;
                &lt;textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"&gt;{{ $product-&gt;detail }}&lt;/textarea&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="col-xs-12 col-sm-12 col-md-12 text-center"&gt;
          &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;

&lt;/form&gt;

@endsection

resources/views/products/show.blade.php

@extends('products.layout')
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2> Show Product</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a>
            </div>
        </div>
    </div>

&lt;div class="row"&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
        &lt;div class="form-group"&gt;
            &lt;strong&gt;Name:&lt;/strong&gt;
            {{ $product-&gt;name }}
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-xs-12 col-sm-12 col-md-12"&gt;
        &lt;div class="form-group"&gt;
            &lt;strong&gt;Details:&lt;/strong&gt;
            {{ $product-&gt;detail }}
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

@endsection

Now we are ready to run our crud application example with Laravel 6 so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/products

You can download code from GitHub

I hope it can help you....

laravel php web-development

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

Top Laravel Development Companies Reviews

Among the list of Best Laravel development companies, TopDevelopers has chosen the most acclaimed laravel developers for the visitors so as to make them avail the extremely wonderful service ever offered. The web development companies listed here...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.