Laravel 6 crud Insert Update Delete operations Example

Laravel 6 crud Insert Update Delete operations Example

Today, We want to share with you Laravel 6 crud Insert Update Delete operations Example and demo.In this post we will show you wordpress plugin require another plugin, hear for CRUD (Create Read Update Delete) in a Laravel 6 App we will give you demo and example for implement.In this post, we will learn about Laravel 6 CRUD (Create Read Update Delete) Tutorial For Beginners with an example.

Laravel 6 crud Insert Update Delete operations Example and demo

There are the Following The simple About Insert Update and Delete records from MySQL with Laravel 6 Full Information With Example and source code.

As I will cover this Post with live Working example to develop How to create CRUD operations (Insert,Update and Delete) in Laravel 6, so the Laravel 6 get data from mysql is used for this example is following below.

Phase 1 : Install Laravel 6

run simple bellow command To fresh Install Laravel 6

composer create-project --prefer-dist laravel/laravel memberProfiles

Phase 2: Laravel 6 Database Configuration

changes some config in .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=memberfamilyinfo
DB_USERNAME=memberfamilyinfo
[email protected]@#$%$*&

Phase 3: Laravel Define a Migration

make a Laravel 6 crud application for member

php artisan make:migration create_members_table --create=members

<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
  
class CreateMembersTable extends Migration
{
    public function up()
    {
        Schema::create('members', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->text('detail');
            $table->timestamps();
        });
    }
  
    public function down()
    {
        Schema::dropIfExists('members');
    }
}

Phase 4: Define a Laravel 6 Resource Route

routes/web.php

Route::resource('members','MemberController');

Phase 5: Add Laravel 6 Controller and Model

php artisan make:controller MemberController --resource --model=Member

app/Http/Controllers/MemberController.php

<?php
  
namespace App\Http\Controllers;
  
use App\Member;
use Illuminate\Http\Request;
  
class MemberController extends Controller
{
    public function index()
    {
        $members = Member::latest()->paginate(5);
        return view('members.index',compact('members'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
   
    public function create()
    {
        return view('members.create');
    }
  
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
  
        Member::create($request->all());
        return redirect()->route('members.index')
                        ->with('success','Member created successfully.');
    }
   
    public function show(Member $member)
    {
        return view('members.show',compact('member'));
    }
   
    public function edit(Member $member)
    {
        return view('members.edit',compact('member'));
    }
  
    public function update(Request $request, Member $member)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
  
        $member->update($request->all());
  
        return redirect()->route('members.index')
                        ->with('success','Member updated successfully');
    }
  
    public function destroy(Member $member)
    {
        $member->delete();
        return redirect()->route('members.index')
                        ->with('success','Member deleted successfully');
    }
}

app/Member.php

<?php
  
namespace App;
use Illuminate\Database\Eloquent\Model;
class Member extends Model
{
    protected $fillable = [
        'name', 'detail'
    ];
}

Phase 6: Add Laravel 6 Blade Files

resources/views/members/layout.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 6 CRUD Application - tamilrokers.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/members/index.blade.php

@extends('members.layout')
 
@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Simple Laravel 6 CRUD Example from scratch - pakainfo.com</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('members.create') }}"> Create New Member</a>
            </div>
        </div>
    </div>
   
    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif
   
    <table class="pakainfo table table-bordered">
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>Details</th>
            <th width="280px">Action</th>
        </tr>
        @foreach ($members as $member)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $member->name }}</td>
            <td>{{ $member->detail }}</td>
            <td>
                <form action="{{ route('members.destroy',$member->id) }}" method="POST">
   
                    <a class="btn btn-info" href="{{ route('members.show',$member->id) }}">Show</a>
    
                    <a class="btn btn-primary" href="{{ route('members.edit',$member->id) }}">Edit</a>
   
                    @csrf
                    @method('DELETE')
      
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>
  
    {!! $members->links() !!}
      
@endsection

resources/views/members/create.blade.php

@extends('members.layout')
  
@section('content')
<div class="row pakainfo">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Add New Member</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-primary" href="{{ route('members.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('members.store') }}" method="POST">
    @csrf
  
     <div class="row pakainfo">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group dsp">
                <strong>Name:</strong>
                <input type="text" name="name" class="form-control" placeholder="Name">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group dsp">
                <strong>Detail:</strong>
                <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea>
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
   
</form>
@endsection

resources/views/members/edit.blade.php

@extends('members.layout')
   
@section('content')
    <div class="row pakainfo">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Edit Member</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('members.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('members.update',$member->id) }}" method="POST">
        @csrf
        @method('PUT')
   
         <div class="row pakainfo">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group dsp">
                    <strong>Name:</strong>
                    <input type="text" name="name" value="{{ $member->name }}" class="form-control" placeholder="Name">
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group dsp">
                    <strong>Detail:</strong>
                    <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{ $member->detail }}</textarea>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12 text-center">
              <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
   
    </form>
@endsection

resources/views/members/show.blade.php

@extends('members.layout')
@section('content')
    <div class="row pakainfo">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2> Show Member</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="{{ route('members.index') }}"> Back</a>
            </div>
        </div>
    </div>
   
    <div class="row pakainfo">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group dsp">
                <strong>Name:</strong>
                {{ $member->name }}
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group dsp">
                <strong>Details:</strong>
                {{ $member->detail }}
            </div>
        </div>
    </div>
@endsection

Web Programming Tutorials Example with Demo

Read :

  Simple Laravel CRUD Validation Tutorial

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel 6 CRUD Application Tutorial.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Reply

avatar
  Subscribe  
Notify of