Home Laravel Laravel 8 Validation Unique with Soft Delete

Laravel 8 Validation Unique with Soft Delete

by Shahriar Sagor

Hello Dev’s
In this tutorial I will show you how to Laravel unique validation also with soft delete. Unique validation in check without delete_at column is not null. On Soft deleted record with the same name exists but not check in a unique validation in laravel.

So we can create categories table in database.

Solution Unique Validation

Step 1 : Create Blade File

Now you can create blade file.


<!DOCTYPE html>
    @if (count($errors) > 0)
            @foreach($errors->all() as $error)
                <li>{{ $error }} </li><br>
    <form action="{{ route('category.store') }}" method="post">
        <input type="text" name="name">
        <input type="text" name="city">
        <label>Mobile No:</label>
        <input type="text" name="mno">
        <button type="submit">Save</button>
    <table border="2" width="50%">
            <th>Mobile No</th>
            <?php $no = 1; ?>
            @foreach($categories as $key => $value)
            <tr style="text-align: center;">
                <td>{{ $no }}</td>
                <td>{{ $value->name }}</td>
                <td>{{ $value->city }}</td>
                <td>{{ $value->mno }}</td>
                <td><a href="{{ route('category.destroy',[$value->id]) }}"><button>Delete</button></a></td>
                <?php $no++; ?>

Step 2 : Create Controller File

So now we can create controller file.



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Category;

class CategoryController extends Controller
    public function index()
        $categories = Category::get();
        return view('category.index',compact('categories'));
     * Store a newly created resource in storage.
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
    public function store(Request $request)

        $input = $request->all();

        return redirect(route('category.index'));
    public function destroy($id)
        return redirect(route('category.index'));

Step 3 : Add Route in web.php

Now In this step, we will add new routes to handle all request.



Read Also : How Laravel Livewire Click Event work?

Thanks for read. I hope it help you. For more you can follow us on facebook


You may also like