Laravel一些优雅的写法

  1. 新增操作
php 复制代码
// 原则,所有服务类只有一个public入口,或者多个public入口,但是他们做都是同一件事情
Class CreateService {


    // 创建类的入口, 根据dto去新建
    public function create(Dto $dto){

        // 先构建model对象, 不要在事务期间构建,减少事务等待
        $good = $this->buildGood($designProduct, $dto);
            $good->setRelation('aList', $this->buildAList($dto));
            $good->setRelation('bList', $this->buildBList($dto));


    DB::transaction(function () use (
            $good
        ) {

             // 保存model对象
               // 主表
                $good->save();
                // 主表关联其他表
                $good->aList()->saveMany(
                    $designReference->aList->all()
                );
                // 主表关联其他表
                $good->bList()->saveMany(
                    $designReference->bList->all()
                );
            // 触发事件,要delay延迟事件,确保事务提交后触发
            delay_event(new GoodCreated());
        });

    }


    // 根据product类去新建
    public function createByProduct(Product $product) {
        // build reference
        // save()
   }


    /**
     * @param Dto $dto
     * @return Collection
     */
    protected function buildAList(
        Dto $dto
    ): Collection {
        $newCollection  = new Collection();
        $$list = array_filter_unique(explode(',', $dto->getName()));

        if (empty($list)) {
            return $newCollection;
        }

        foreach ($list as $name) {
            $newCollection->add(new GoodAList([
                'name'    => $name,
            ]));
        }

        return $newCollection;
    }

    protected function buildBList(
        Dto $dto
    ): Collection {
        $newCollection  = new Collection();

        if (!empty($dto->getBName())) {
            $newCollection->add(
                new GoodBList(
                    [
                        'country_name'    => $dto->getBName(),
                    ]
                )
            );
        }

        return $newCollection;
    }


    /**
     * @param Product $product
     * @param Dto $dto
     * @return Good
     */
    protected function buildGood(
        Product $product,
        Dto $dto
    ): Good {
        return new Good([
          
            'updated_at'   => $dto->getDataUpdatedAt(),
            'band_id'   => $product->band_id,
        ]);
    }

}

$service = new CreateService();
$service->create();
相关推荐
JaguarJack2 小时前
免费可商用 PHP 管理后台 CatchAdmin V5.4.1 发布,新增 MCP 支持
后端·php·laravel·服务端
魔众1 天前
ModStartBlog v11.3.0 安全加固、体验优化,自动化测试更强大
开源·php·laravel
AI情绪识别开源7 天前
HFpEF 心房-心室耦联效率无创监测方向(AVCEI)
typescript·laravel·perl
catchadmin8 天前
用 Jev 与 Laravel AI SDK 检测垃圾邮件和自动回复
数据库·人工智能·laravel
BingoGo13 天前
用 Jev 与 Laravel AI SDK 检测垃圾邮件和自动回复
后端·laravel
JaguarJack13 天前
用 Jev 与 Laravel AI SDK 检测垃圾邮件和自动回复
后端·ai·php·laravel·服务端
右耳朵猫AI22 天前
PHP周刊2026W38 | Laravel AI可观测、Symfony三版本、Webhook零信任、事件溯源架构
后端·php·laravel
右耳朵猫AI1 个月前
PHP周刊2026W37 | Symfony 三维护版齐发、Laravel AI SDK 0.11、LSP 服务器上线
后端·php·laravel
右耳朵猫AI1 个月前
PHP周刊2026W36 | Laravel AI SDK 0.11可观测、Symfony 8.1.5、Octane并发、事件溯源
人工智能·php·laravel
JaguarJack1 个月前
现在就值得尝试的 5 个 Laravel 新包
后端·php·laravel·服务端