Laravel 7 Soft Delete With Unique Validation

Laravel 7 Soft Delete With Unique Validation

In this laravel soft delete with unique validation example, you will learn how to use soft delete with unique validation in laravel.

Sometime, you need to create or update data into database table. And you want to use unique validation with soft delete in laravel.

So this tutorial will guide you on how to use soft delete unique validation with inserting and updating data into database tables in laravel.

Before taking a look at the examples, first of you need to know how to use soft delete.

When models are soft deleted, they are not actually removed data from your database table. Instead, a timestamp is set on the deleted_at column. If a model has a non-null deleted_at value, the model has been soft deleted.

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

This tutorial provides you some examples of soft delete with unique validation in laravel apps.

Here, we will discuss two examples of soft delete with unique valiation on insert data into DB and as well as update DB table in laravel apps.

Now, let’s take a look at examples of soft delete with unique validation as follow:

Example 1: On inserting data

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

public function update(Request $request, $id)

{

    $request->validate([

        'name'=>'required|unique:form_types,name,'.$id.',id,deleted_at,NULL',

    ]);

    // Write Code here

}

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *