laravel神器教你一秒搞定增删改查业务模块-composer包query-common

本文主要是介绍laravel神器教你一秒搞定增删改查业务模块-composer包query-common,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

laravel神器教你一秒搞定增删改查业务模块

还在为了不断写增删改查而烦恼不堪嘛?还在为了重复写代码而头疼嘛?这个laravel神器拯救你的大脑,解放你的双手。让你有更多的时间去写出更好的代码。

安装

首先使用composer安装

composer require thepatter/query-common

安装之后创建一个command

php artisan make:command MakeQueryCommand

把下面的内容复制粘贴进去

<?phpnamespace App\Console\Commands;use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;class MakeQueryCommand extends GeneratorCommand
{/*** The console command name.** @var string*/protected $name = 'make:queryController';/*** The console command description.** @var string*/protected $description = 'Create a new queryController class';/*** The type of class being generated.** @var string*/protected $type = 'QueryController';/*** Get the stub file for the generator.** @return string*/protected function getStub(){return resource_path('stubs/queryController.stub');}/*** Get the default namespace for the class.** @param  string  $rootNamespace* @return string*/protected function getDefaultNamespace($rootNamespace){return $rootNamespace.'\Http\Controllers';}/*** Build the class with the given name.** Remove the base controller import if we are already in base namespace.** @param  string  $name* @return string*/protected function buildClass($name){$controllerNamespace = $this->getNamespace($name);$replace = [];if ($this->option('model')) {$replace = $this->buildModelReplacements($replace);}$replace["use {$controllerNamespace}\Controller;\n"] = '';return str_replace(array_keys($replace), array_values($replace), parent::buildClass($name));}/*** Build the model replacement values.** @param  array  $replace* @return array*/protected function buildModelReplacements(array $replace){$modelClass = $this->parseModel($this->option('model'));if (! class_exists($modelClass)) {if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) {$this->call('make:model', ['name' => $modelClass]);}}return array_merge($replace, ['DummyFullModelClass' => $modelClass,]);}/*** Get the fully-qualified model class name.** @param  string  $model* @return string*/protected function parseModel($model){// if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {//     throw new InvalidArgumentException('Model name contains invalid characters.');// }$model = trim(str_replace('/', '\\', $model), '\\');if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) {$model = $rootNamespace.$model;}return $model;}/*** Get the console command options.** @return array*/protected function getOptions(){return [['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a query controller for the given model.'],];}
}

在resources文件夹下创建stubs文件夹,在stubs文件夹下面创建QueryController.stub文件,把下面内容复制粘贴进去

<?phpnamespace DummyNamespace;use Illuminate\Http\Request;
use DummyRootNamespaceHttp\Controllers\QueryList\QueryController;
use App\Exceptions\CommonException;class DummyClass extends QueryController
{/*** 字典数组* ['表里的字段名' => '字典code',...]*/protected $dicArr = [];/*** 字段映射 可选,不填默认转成下划线格式* ['搜索字段' => '表字段',...]*/protected $filedsAdapter = [];/*** 创建时候的字段映射 可选,不填默认转成下划线格式* ['输入字段' => '表字段']*/protected $createAdapter = [];//定义表名 格式: table as tprotected $shortTableName;protected function getModel() {return new "DummyFullModelClass";}/** 查询列表* @route get.api/lists*/public function getList(Request $request){try{//检查页码,搜索条件等$this->pageValid();//返回数据return $this->success($this->pageList());} catch (Exception $ex) {}}  /*** 创建* @route post.api/info*/function createInfo(Request $request) {try{//创建$this->create($request->all());return $this->success(true);}catch(Exception $ex) {}}/*** 更新* @route put.api/info/{id}*/function updateInfo(Request $request, $id) {try{//查询记录$detail = $this->getModel()->find($id);if (empty($detail)) {//补充错误信息throw new CommonException();}//更新$this->update($id,$request->all());return $this->success(true);}catch(Exception $ex) {}}/*** 查询一条记录* @route get.api/info*/function detail(Request $request) {try{$rules = ['id'=>'required',];$messages = ['id.required'=>'id为必填项',];//验证$this->valid($request, $rule, $messages);//查询记录$detail = $this->getModel()->find($id);if (empty($detail)) {//补充错误信息throw new CommonException();}return $this->success($detail);}catch(Exception $ex) {}}/*** 删除一条记录* @route delete.api/info/{id}*/function deleteInfo(Request $request, $id) {try{//查询记录$model = $this->getModel();$detail = $model->find($id);if (empty($detail)) {//补充错误信息throw new CommonException();}//进行删除$res = $model->where('id', $id)->delete();return $this->success(true);}catch(Exception $ex) {}}}

创建业务逻辑

这时候执行创建的artisan命令就可以了

php artisan make:queryController your controller path -m your model path

这时候在你的Controller下面就会多出一个Controller文件,你只需要在路由中添加路由就可以了。

这个库的github地址在下面,感兴趣的朋友可以看一下。

https://github.com/Thepatterraining/queryCommon

这篇关于laravel神器教你一秒搞定增删改查业务模块-composer包query-common的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/857032

相关文章

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

python中time模块的常用方法及应用详解

《python中time模块的常用方法及应用详解》在Python开发中,时间处理是绕不开的刚需场景,从性能计时到定时任务,从日志记录到数据同步,时间模块始终是开发者最得力的工具之一,本文将通过真实案例... 目录一、时间基石:time.time()典型场景:程序性能分析进阶技巧:结合上下文管理器实现自动计时

Spring AI集成DeepSeek三步搞定Java智能应用的详细过程

《SpringAI集成DeepSeek三步搞定Java智能应用的详细过程》本文介绍了如何使用SpringAI集成DeepSeek,一个国内顶尖的多模态大模型,SpringAI提供了一套统一的接口,简... 目录DeepSeek 介绍Spring AI 是什么?Spring AI 的主要功能包括1、环境准备2

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate