Laravel 8 Validation Unique with Soft Delete

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

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.

resources\views\category\index.blade.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    @if (count($errors) > 0)
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }} </li><br>
            @endforeach 
        </ul>
    @endif
    <form action="{{ route('category.store') }}" method="post">
        @csrf
        <label>Name:</label>
        <input type="text" name="name">
        <br>
        <br>
        <label>City:</label>
        <input type="text" name="city">
        <br>
        <br>
        <label>Mobile No:</label>
        <input type="text" name="mno">
        <br>
        <br>
        <button type="submit">Save</button>
    </form>
    <br>
    <br>
    <br>
    <br>
    <table border="2" width="50%">
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>City</th>
            <th>Mobile No</th>
            <th>Action</th>
        </tr>
            <?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++; ?>
            </tr>
            @endforeach
    </table>
</body>
</html>

Step 2 : Create Controller File

So now we can create controller file.

App/http/controller/CategoryController

<?php

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)
    {
        $request->validate([
            'name'=>'required',
            'city'=>'required|unique:categories,city,Null,id,deleted_at,NULL',
            'mno'=>'required|numeric',
        ]);

        $input = $request->all();

        Category::create($input);        
        return redirect(route('category.index'));
    }
    public function destroy($id)
    {
        Category::find($id)->delete();
        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.

routes\web.php

Route::get('category','CategoryController@index')->name('category.index');
Route::post('category','CategoryController@store')->name('category.store');
Route::delete('category/{id}','CategoryController@destroy')->name('category.destroy');

Read Also : How Laravel Livewire Click Event work?

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

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →