定制 ferdinandbr/laravel-model-unique-field 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ferdinandbr/laravel-model-unique-field

最新稳定版本:v1.0.0

Composer 安装命令:

composer require ferdinandbr/laravel-model-unique-field

包简介

create a record with a unique field without worrying

README 文档

README

The ferdinandbr/laravel-model-unique-field package provides a simple way to handle dynamically unique fields in Laravel models. When creating or updating a model with a field that needs to be unique, the package automatically appends a numeric suffix if a duplicate value is detected. This allows you to easily manage unique fields like names, serial numbers, or other attributes without manually checking for duplicates.

For example, if a model is created with a name "Product A" and another model is created with the same name, the second model’s name will automatically be "Product A #2", and so on. This feature is especially useful when dealing with large datasets, imports, or batch operations where conflicts might occur.

The package is customizable and can be applied to any model attribute, ensuring smooth management of unique field values across your application.

Requirement

  • Minimum PHP ^8.0

Installation

You can install the package via composer for latest version

composer require ferdinandbr/laravel-model-unique-field

Quick usage

Add UniqueField trait to model and add $dynamicField to field referencing the column you want the value to not be repeated

use Ferdinandbr\LaravelModelUniqueField\Traits\UniqueField;

class TestModel extends Model
{
    use UniqueField;

    protected $dynamicField = 'name';

}

Examples

use App\Models\Product;

$product1 = Product::create(['name' => 'Widget']);
$product2 = Product::create(['name' => 'Widget']);
$product3 = Product::create(['name' => 'Widget']);

echo $product1->name; // Output: "Widget"
echo $product2->name; // Output: "Widget #2"
echo $product3->name; // Output: "Widget #3"


use App\Models\Device;

$device1 = Device::create(['serial_number' => 'SN123']);
$device2 = Device::create(['serial_number' => 'SN123']);
$device3 = Device::create(['serial_number' => 'SN123']);

echo $device1->serial_number; // Output: "SN123"
echo $device2->serial_number; // Output: "SN123 #2"
echo $device3->serial_number; // Output: "SN123 #3"

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-01