waad/laravel-duplicated-unique-soft-delete

Add media to your Laravel application in one place

v1.0.0 2023-04-01 11:51 UTC

This package is not auto-updated.

Last update: 2024-11-19 11:17:43 UTC


README

Banner

📌 Duplicated Unique Soft Delete Package

Deleting unique columns using soft delete is not a recommended method but works in some cases where the data is not important.

 

🧔 Authors

 

🎈 Mini Requirement

  • PHP >= 8.0.0

 

💯 Installation

Installation By Composer

composer require waad/laravel-duplicated-unique-soft-delete

 

🧰 Usage

In Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Waad\DUSD\DeleteUniqueable;

class Post extends Model
{
    use SoftDeletes;
    use DeleteUniqueable;


    // Important: Unique columns to check
    public $unique_attributes = ['serial_number', 'mac_address'];
    

 

  • We have to parameters to help your choice in request

1 - unique_or_operator

It does not check all unique columns, it must be variable, but it takes the least consideration, which is at least one variable column, to delete the old one and enter the new one.

values = 1 or true

 

2 - unique_select_attributes

It does not check all unique columns specified in Moodel but only those specified here in this parameter.

eg. values = serial_number,mac_address

 

⚠️ Note must remove unique validation from request form of controller

eg.

public function rules()
{
    return [
        'name' => 'required|string|max:255',
        'serial_number' => 'required|string|max:255,
        'mac_address' => 'required|string|max:255',
    ];
}

Instead Of

public function rules()
{
    return [
        'name' => 'required|string|max:255',
        'serial_number' => 'required|string|max:255|unique:items,serial_number',
        'mac_address' => 'required|string|max:255|unique:items,mac_address',
    ];
}

 

🎀 Example

  • POST method created example
name:Item 3
serial_number:s6
mac_address:m6

 

use parameters

name:Item 3
serial_number:s6
mac_address:m6
unique_select_attributes:serial_number
unique_or_operator:1

⚠️ when use unique_or_operator here will check only serial_number and use or in checking regardless mac_address if exist with serial_number in same record

 

🎯 License

MIT License