CRUD Operations in Laravel 7 PHP Framework

Today, We want to share with you CRUD Operations in Laravel 7 PHP Framework.In this post we will show you CRUD (Create Read Update Delete) in a Laravel 7 App, hear for Laravel 7 CRUD Tutorial Example Step By Step From Scratch we will give you demo and example for implement.In this post, we will learn about Simple Laravel 7 CRUD with Resource Controllers with an example.

CRUD Operations in Laravel 7 PHP Framework

There are the Following The simple About Step by Step CRUD Operation in Laravel7 with File Upload Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 7 Tutorial For Beginners, so the laravel resource collection is used for this example is following below.

Phase 1 : Install Laravel 7

get fresh Laravel 7 version application

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

Phase 2: Database Configuration

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=DB your name(booster_v1)
DB_USERNAME=DB username(root)
DB_PASSWORD=DB password(root)

Phase 3: Make Migration

create migration for “websites” table

php artisan make:migration create_websites_table --create=websites

path “database/migrations”

<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
  
class CreateWebsitesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('websites', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('detail');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('websites');
    }
}

Phase 4: Add Resource Route

routes/web.php

Route::resource('websites','WebsiteController');

Phase 5: Add Controller and Model

create new controller as WebsiteController

php artisan make:controller WebsiteController --resource --model=Website

app/Http/Controllers/WebsiteController.php

<?php
  
namespace App\Http\Controllers;
  
use App\Website;
use Illuminate\Http\Request;
  
class WebsiteController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $websites = Website::latest()->paginate(5);
  
        return view('websites.index',compact('websites'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
   
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('websites.create');
    }
  
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
  
        Website::create($request->all());
   
        return redirect()->route('websites.index')
                        ->with('success','Website created successfully.');
    }
   
    /**
     * Display the specified resource.
     *
     * @param  \App\Website  $website
     * @return \Illuminate\Http\Response
     */
    public function display(Website $website)
    {
        return view('websites.display',compact('website'));
    }
   
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Website  $website
     * @return \Illuminate\Http\Response
     */
    public function edit(Website $website)
    {
        return view('websites.edit',compact('website'));
    }
  
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Website  $website
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Website $website)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
        ]);
  
        $website->update($request->all());
  
        return redirect()->route('websites.index')
                        ->with('success','Website updated successfully');
    }
  
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Website  $website
     * @return \Illuminate\Http\Response
     */
    public function destroy(Website $website)
    {
        $website->delete();
  
        return redirect()->route('websites.index')
                        ->with('success','Website deleted successfully');
    }
}

app/Website.php

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

Phase 6: Add Blade Files

resources/views/websites/layout.blade.php

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

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

resources/views/websites/create.blade.php

@extends('websites.layout')
  
@section('content')
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>Add New Website</h2>
        </div>
        <div class="pull-right">
            <a class="btn btn-default" href="{{ route('websites.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('websites.store') }}" method="POST">
    @csrf
  
     <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="jkrd form-control dsp">
                <strong>Name:</strong>
                <input type="text" name="name" class="bbrp form-control jdk" placeholder="Name">
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="jkrd form-control dsp">
                <strong>Detail:</strong>
                <textarea class="bbrp form-control jdk" 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-default">Submit</button>
        </div>
    </div>
   
</form>
@endsection

resources/views/websites/edit.blade.php

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

resources/views/websites/display.blade.php

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

Web Programming Tutorials Example with Demo

Read :

Read Also:  Creating PDF Files using PHP FPDF library - Convert HTML to PDF

Summary

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

I hope you get an idea about laravel crud generator.
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.