Hey, dude, if you wanna know about laravel unique validation with soft delete then you are at the right place.
If you have added soft deleted to your laravel models and now have some unique columns in your database table. Then it can create an issue for you.
Let’s suppose you have unique tokens in your table. Now you just delete the one record from your table. After this if you try to add the new record with the same token which you have just deleted then it will give you an error.
“The record is already existed. “
Here is what happened. By default for unique validation it just checks the whole table with trashed (soft deleted) records.
But we want to check only non-deleted records or the records which are not soft deleted and available to use.
deleted_at = Null
for unique validation. By doing this we can leave the trashed records.deleted_xyzabc
or xyzabc_deleted
.First example to use Laravel unique validation with soft delete is using Form Requests.
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class UserTokensRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'user_id' => 'required', 'token'=>'required|unique:user_tokens,token,{$this->id},id,deleted_at,NULL' ]; } }
If you wanna use Rule facades you can replace the token line with below code.
'token' => ['required', Rule::unique('user_tokens')->ignore($this->id)->whereNull('deleted_at')]
SELECT COUNT(*) AS aggregate FROM user_tokens WHERE token = ‘xyzabc’ AND id <> 1 or Null;
As you can see in the above query it will check all records of the table including soft deleted records.
2. But after implementing this solution the below query will be run where it will check the deleted_at
column to identify the deleted records.
SELECT COUNT(*) AS aggregate FROM user_tokens WHERE token = ‘xyzabc’ AND id <> 1 or Null AND deleted_at IS NULL;
So, you wanna know the second solution to get rid of this laravel unique validation with soft delete issue.
For this follow the below steps
php artisan make:observer <observerName> -m <ModelName>;
It will create the observer and attach the models to each event. After this don’t forget to correct the model namespace if you have models in another directory.
Our example,
php artisan make:observer UserTokenObserver -m UserToken
App\Providers\EventServiceProvider
and in the boot method add the below code. It will connect the observer to your model.public function boot() { Model::observe(ModelObserver::class); }
Replace the Model with your model name ModelObserver with your model observer and make sure the namespace should be correct for both model and model observer.
Our example,
Use App\Model\UserToken; Use App\Observers\UserTokenObserver; class EventServiceProvider extends ServiceProvider { /** * Register any events for your application. * * @return void */ public function boot() { UserToken::observe(UserTokenObserver::class); } }
<?php namespace App\Observers; Use App\Model\UserToken; class UserTokenObserver { /** * Handle the UserToken "created" event. * * @param \App\UserToken $userToken * @return void */ public function created(UserToken $userToken) { // } /** * Handle the UserToken "updated" event. * * @param \App\UserToken $userToken * @return void */ public function updated(UserToken $userToken) { // } /** * Handle the UserToken "deleted" event. * * @param \App\UserToken $userToken * @return void */ public function deleted(Service $service) { } public function deleting(UserToken $userToken) { $userToken->token = 'deleted_'.$userToken->token; $userToken->save(); } /** * Handle the UserToken "restored" event. * * @param \App\UserToken $userToken * @return void */ public function restored(Service $service) { // } /** * Handle the UserToken "force deleted" event. * * @param \App\UserToken $userToken * @return void */ public function forceDeleted(Service $service) { // } }
That’s it,
How to use laravel unique validation with soft delete.
Hope you find this article useful. See you in the next learning chapter.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…