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();
相关推荐
wendyNo9 天前
Laravel 结合影刀 RPA 实现企业微信自动询单报价流程
企业微信·laravel·rpa
2501_938791229 天前
PHP Laravel 10 框架:使用队列处理异步任务(邮件发送 / 数据导出)
android·php·laravel
JaguarJack10 天前
Laravel 新项目避坑指南10 大基础设置让代码半年不崩
后端·php·laravel
stark张宇11 天前
高手项目:手把手带你构建AI办公应用,赋能企业数字化
ai编程·laravel·swoole
JaguarJack11 天前
开发者必看的 15 个困惑的 Git 术语(以及它们的真正含义)
后端·php·laravel
JaguarJack12 天前
2025 年必须尝试的 5 个 Laravel 新特性
后端·php·laravel
andux18 天前
layuiadmin与laravel 12 前后端分离nginx配置
php·layui·laravel·layuiadmin
玩转C语言和数据结构19 天前
Laravel下载和安装图解(非常详细)
laravel·laravel下载和安装·laravel安装教程·laravel下载和安装教程·laravel框架下载·php laravel框架·laravel下载
JaguarJack21 天前
PHP 桌面端框架NativePHP for Desktop v2 发布!
后端·php·laravel
yyxl__jyj1 个月前
Java异步编程实战CompletableFuture原理深度解析与性能优化技巧
laravel