Create Routes

routes/web.php : In next step, we will add new two routes in web.php file.

Route::get('/', function () {
    return view('registration');
});
Route::post('crud_insert', 'CrudController@save');
Route::get('crud','CrudController@crud');
Route::get('crud_delete/{id}', 'CrudController@crud_delete');
Route::get('crud_update/{id}','CrudController@crud_update');

Creating The CrudController

Create a controller file CrudController.php and save it in the following path app/Http/Controllers/CrudController.php.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\CrudModel;

class CrudController extends Controller
{
    function crud(){
        $data['data'] = CrudModel::all();
        return view('crud',$data);
    }
    
    function save(Request $req){
        $data = new CrudModel;
        if(!empty($req->id)){
            $data->where("id",$req->id)
                    ->update(['name' => $req->name,'email' => $req->email,'password' => $req->pwd]);
            return back()
                ->with("status","Update Successfully.");
        }else{
            $data = new CrudModel;
            $data->name = $req->name;
            $data->email = $req->email;
            $data->password = $req->pwd;
            $data->save();
            return back()
                ->with("status","Insert Successfully.");
        }
    }
    
    function crud_delete($id){
        $crud = CrudModel::find($id);
        $crud->delete();
        
        return back()
            ->with("status","Delete Successfully.");
    }
    function crud_update($id){
        $crud['data'] = CrudModel::find($id)->first();
        return view("registration",$crud);
    }
}
?>

Creating The CrudModel

Create a controller file CrudModel.php and save it in the following path app/CrudModel.php.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CrudModel extends Model
{
    protected $table = 'demo';
    public $timestamps = false;
}
?>

Creating The View

Create a view file crud.blade.php and save the below code it in resources/views/crud.blade.php.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Crud Form</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="https://www.jqueryscript.net/demo/Simple-Easy-jQuery-Notification-Plugin-NotifIt/css/notifIt.css">
  <script src="https://www.jqueryscript.net/demo/Simple-Easy-jQuery-Notification-Plugin-NotifIt/js/notifIt.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container-fluid mt-5">
    <h1 class="text-center">Crud Form</h1>
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            @csrf
            @if ($message = Session::get('status'))
            <div class="alert alert-success alert-block">
                <button type="button" class="close" data-dismiss="alert">×</button>
                    <strong>{{ $message }}</strong>
            </div>
            @endif
            <table class="table table-dark table-striped mt-3">
                <thead>
                  <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Password</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                    @if(!empty($data))
                        @foreach($data as $crud)
                  <tr>
                    <td>{{ $crud->name }}</td>
                    <td>{{ $crud->email }}</td>
                    <td>{{ $crud->password }}</td>
                    <td>
                        <a class="btn crud_delete btn-danger mr-3" href="crud_delete/{{$crud->id}}"> X </a>
                        <a href="crud_update/{{$crud->id}}" class="btn btn-warning"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
                    </td>
                  </tr>
                        @endforeach
                    @endif
                </tbody>
              </table>
        </div>
    </div>
</div>
</body>
</html>

Modify The View

Modify a view file registration.blade.php and save the below code it in resources/views/registration.blade.php.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Registration Form</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container mt-5">
    <h1 class="text-center">Registration Form</h1>
   <form action="<?= (isset($data->id)) ? '../crud_insert': 'crud_insert'; ?>" method="post">
    @csrf
    @if ($message = Session::get('status'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
            <strong>{{ $message }}</strong>
    </div>
    @endif 
    <input type="hidden" name="id" value="<?= (isset($data->id)) ? $data->id : ''; ?>">
    
    <label for="name" class="mb-2 mr-sm-2">Name:</label>
    <input type="text" class="form-control mb-2 mr-sm-2" id="name" placeholder="Enter name" name="name" value="<?= (isset($data->name)) ? $data->name : ''; ?>">
    <label for="email2" class="mb-2 mr-sm-2">Email:</label>
    <input type="text" class="form-control mb-2 mr-sm-2" id="email2" placeholder="Enter email" name="email" value="<?= (isset($data->email)) ? $data->email : ''; ?>">
    <label for="pwd2" class="mb-2 mr-sm-2">Password:</label>
    <input type="text" class="form-control mb-2 mr-sm-2" id="pwd2" placeholder="Enter password" name="pwd" value="<?= (isset($data->password)) ? $data->password : ''; ?>">
    <input type="submit" class="btn btn-primary mb-2" value="Insert">
  </form>
</div>

</body>
</html>

Let us execute this example by the following URL in the browser. This URL may be different based on your website.

https://howtowebcode.com/crud