In this tutorial you will learn about the Laravel 7 Soft Delete With Unique Validation and its application with practical example.
In this Laravel 7 Soft Delete With Unique Validation I’ll show you how add soft delete with unique validation in laravel. In this tutorial you will learn to implement and use soft delete property in laravel. In this step by step guide I’ll demonstrate you to implement and use soft delete in laravel project.
Table Of Contents−
Laravel 7 Soft Delete With Unique Validation
1 2 3 4 5 6 7 8 9 10 11 12 13 |
use Illuminate\Database\Eloquent\SoftDeletes; class User extends Model { use SoftDeletes; public $timestamps = true; protected $fillable = [ 'name', 'email', 'created_by', 'updated_by', ]; } |
Laravel Unique Validation with Soft Delete
In this example you will learn to add unique validation with soft delete on inserting of data in laravel.
Example 1: On inserting data
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function store(Request $request) { $request->validate([ 'name'=>'required|unique:form_types,name,NULL,id,deleted_at,NULL', ]); // Write your code here } |
Example 2: On updating data
In this example you will learn to add unique validation with soft delete on updating of data in laravel.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function update(Request $request, $id) { $request->validate([ 'name'=>'required|unique:form_types,name,'.$id.',id,deleted_at,NULL', ]); // Write Code here } |