Today, We want to share with you Laravel Soft Delete Unique validations.In this post we will show you Laravel Validation Unique Soft Delete Example, hear for Soft Delete Cascade using Laravel Eloquent Example we will give you demo and example for implement.In this post, we will learn about how to make unique validations work with soft delete in laravel with an example.
Laravel Soft Delete Unique validations
There are the Following The simple About Laravel Soft Delete Unique validations Full Information With Example and source code.
As I will cover this Post with live Working example to develop Unique Database Fields with Laravel Soft Deletes, so the PHP Laravel Soft Delete Migration cascade for this example is following below.
Laravel Validation Unique With Soft Delete
I display you both example below source code, One is for Mysql create time and another is on edit time on Laravel controller validation.
Laravel Soft Delete Unique On Create Method:
public function store(Request $request) { $request->validate([ 'name'=>'required|unique:products,name,NULL,id,deleted_at,NULL', ]); }
Laravel Soft Delete Unique On Update Method:
public function update(Request $request, $id) { $request->validate([ 'name'=>'required|unique:products,name,'.$id.',id,deleted_at,NULL', ]); }
Example 2: laravel unique ignore soft deleted
Unique Validations Work With Soft Delete In Laravel
public function up() { Schema::create('visitors', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email'); $table->string('password'); $table->rememberToken(); $table->timestamps(); $table->softDeletes(); $table->unique(['email', 'deleted_at']); }); }
soft deleted user email available
'email' => 'required|email|max:45|unique:visitors,email,NULL,id,deleted_at,NULL',
laravel validation unique update & Create
On Insert Soft Delete In Laravel:
public function store(Request $request) { //Check Validation for products request()->validate([ 'name' => 'required|min:5|max:50|unique:products,name,NULL,id,deleted_at,NULL', 'category_name' => 'required|min:2|max:20|unique:products,category_name,NULL,id,deleted_at,NULL', 'branch_prefix' => 'required|min:2|max:20|unique:products,branch_prefix,NULL,id,deleted_at,NULL', 'owner_url' => 'required|min:2|max:255|unique:products,owner_url,NULL,id,deleted_at,NULL', 'product_doc_url' => 'required|min:2|max:255|unique:products,product_doc_url,NULL,id,deleted_at,NULL', ]); $data = $request->only([ 'name', 'category_name', 'branch_prefix', 'owner_url', 'product_doc_url', ]); Products::create($data); return redirect('products')->with('success','product created successfully.'); }
On Update Soft Delete In Laravel:
public function update(Request $request, $id = 0) { request()->validate([ 'name' => 'required|unique:products,name,'.$id.',id,deleted_at,NULL', 'category_name' => 'required|min:2|max:20|unique:products,category_name,'.$id.',id,deleted_at,NULL', 'branch_prefix' => 'required|min:2|max:20|unique:products,branch_prefix,'.$id.',id,deleted_at,NULL', 'owner_url' => 'required|min:2|max:255|unique:products,owner_url,'.$id.',id,deleted_at,NULL', 'product_doc_url' => 'required|min:2|max:255|unique:products,product_doc_url,'.$id.',id,deleted_at,NULL', ]); $data = $request->only([ 'name', 'category_name', 'branch_prefix', 'owner_url', 'product_doc_url', ]); $products= \App\Products::find($id); $products->name=$request->get('name'); $products->category_name=$request->get('category_name'); $products->branch_prefix=$request->get('branch_prefix'); $products->owner_url=$request->get('owner_url'); $products->product_doc_url=$request->get('product_doc_url'); $products->save(); // return redirect('products'); return redirect('products') ->with('success','Product updated successfully.'); }
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Laravel Soft Delete Unique validations.
I would like to have feedback on my Pakainfo.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.