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();
相关推荐
siner.li2 小时前
laravel的Redis锁实现
laravel
Shi_haoliu4 天前
SolidTime 本地与服务器环境搭建指南(Laragon + Laravel + Vue3 + PostgreSQL)
服务器·vue.js·ai·postgresql·php·laravel
是乐乐啊呀10 天前
laravel
php·laravel
小代码201612 天前
ubuntu vscode docker php 环境搭建
vscode·ubuntu·docker·php·laravel
lskblog13 天前
PHP中正确处理HTTP响应:从原始响应到JSON数组的完整指南
http·json·php·laravel
JaguarJack13 天前
前后端分离框架 CatchAdmin V5 beta.2 发布 插件化与开发效率的进一步提升
后端·php·laravel
catchadmin15 天前
使用 Laravel Workflow 作为 MCP 工具提供给 AI 客户端
人工智能·php·laravel
catchadmin17 天前
当遇见 CatchAdmin V5-模块化设计重新定义 Laravel 后台开发
php·laravel
北漂燕郊杨哥17 天前
Laravel中Tymon\JWTAuth 的用法示例
php·laravel
BingoGo18 天前
使用 Laravel Workflow 作为 MCP 工具提供给 AI 客户端
后端·php·laravel