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();
相关推荐
lskblog12 小时前
Composer安装教程及国内镜像设置(含腾讯云、阿里云镜像)
阿里云·php·腾讯云·laravel·composer
四季豆豆豆2 天前
办公任务分发项目 laravel vue mysql 第一章:核心功能构建 API
vue.js·mysql·laravel
lskblog3 天前
使用 PHP Imagick 扩展实现高质量 PDF 转图片功能
android·开发语言·前端·pdf·word·php·laravel
喔烨鸭9 天前
前后端分离情况下,将本地vue项目和Laravel项目以及mysql放到自己的云服务器
vue.js·mysql·laravel
深兰科技12 天前
深兰科技:搬迁公告,我们搬家了
javascript·人工智能·python·科技·typescript·laravel·深兰科技
分享点15 天前
Laravel 使用阿里云OSS S3 协议文件上传
阿里云·php·laravel
大熊不是猫16 天前
Laravel 事件与监听器
php·laravel·event
zh73141 个月前
laravel在cli模式下输出格式漂亮一些
microsoft·php·laravel
wuzuyu3651 个月前
Laravel The requested URL /hellowzy was not found on this server. 404 问题的解决
php·laravel
奔跑吧邓邓子1 个月前
从0到1学PHP(十二):PHP 框架入门与项目实战
php·框架·laravel·项目实战·thinkphp·yii