定制 effectra/laravel-model-operations 二次开发

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

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

effectra/laravel-model-operations

最新稳定版本:v1.0.2

Composer 安装命令:

composer require effectra/laravel-model-operations

包简介

A Laravel package for model operations.

README 文档

README

LaravelModelOperations is a lightweight package that provides reusable traits for performing common Eloquent model operations in Laravel, such as creating, updating, deleting, and handling bulk actions.

It helps keep your controllers and services clean by centralizing repetitive model logic into reusable traits.

Installation

You can install the package via Composer:

composer require effectra/laravel-model-operations

Features

  • 🎯 Simplified model operations (create, createMany, etc.)
  • 🔄 Bulk operations with error handling
  • 🛡️ Strong typing & exceptions for better safety
  • 🧩 Easy to extend with your own logic

The UseCreate Trait

The UseCreate trait provides methods to create single or multiple model records in a clean and reusable way.

Importing the trait

use LaravelModelOperations\Traits\UseCreate;

class UserService
{
    use UseCreate;

    protected string $model = \App\Models\User::class;
}

Creating a single record

// In a controller or service:
$userService = new UserService();

// Example request validation
$request = new \Illuminate\Http\Request([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'password' => bcrypt('secret'),
]);

// Create user
$created = $userService->create($request);

if ($created) {
    $user = $userService->getModelCreated();
    dump("User created:", $user->toArray());
}

What happens here?

  • The data is validated ($request->validated()) before being used.
  • A new User is created and stored in $userService->getModelCreated().
  • If creation fails, it simply returns false.

Creating with default attributes

Sometimes you want to add default values during creation:

$created = $userService->create(
    $request,
    ['status' => 'active'] // default values
);

This will merge defaults with the validated request before saving.

Adding a callback after creation

You can pass a closure that runs after successful save:

$created = $userService->create($request, [], function ($user) {
    // Send a welcome email
    \Mail::to($user->email)->send(new \App\Mail\WelcomeMail($user));
});

Creating multiple records at once

$request = new \Illuminate\Http\Request([
    [
        'name' => 'User 1',
        'email' => 'user1@example.com',
        'password' => bcrypt('secret'),
    ],
    [
        'name' => 'User 2',
        'email' => 'user2@example.com',
        'password' => bcrypt('secret'),
    ]
]);

$success = $userService->createMany($request);

if ($success) {
    echo "All users created successfully!";
} else {
    echo "Some users failed to create. Failed index: " . $userService->modelFailedIndex;
}

What happens here?

  • Each item in the request array is passed to create().
  • If all succeed → returns true.
  • If one fails → returns false and stores the failed index for debugging.

Exception Handling

When bulk creation fails, a ManyOperationException can be thrown with the index of the failed item.

try {
    $success = $userService->createMany($request);
} catch (\ManyOperationException $e) {
    logger("Failed at index: " . $e->getIndex());
}

License

This package is open-sourced under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-21