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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Nginx添加内置模块过程

《Nginx添加内置模块过程》文章指导如何检查并添加Nginx的with-http_gzip_static模块:确认该模块未默认安装后,需下载同版本源码重新编译,备份替换原有二进制文件,最后重启服务验... 目录1、查看Nginx已编辑的模块2、Nginx官网查看内置模块3、停止Nginx服务4、Nginx

python urllib模块使用操作方法

《pythonurllib模块使用操作方法》Python提供了多个库用于处理URL,常用的有urllib、requests和urlparse(Python3中为urllib.parse),下面是这些... 目录URL 处理库urllib 模块requests 库urlparse 和 urljoin编码和解码

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

idea Maven Springboot多模块项目打包时90%的问题及解决方案

《ideaMavenSpringboot多模块项目打包时90%的问题及解决方案》:本文主要介绍ideaMavenSpringboot多模块项目打包时90%的问题及解决方案,具有很好的参考价值,... 目录1. 前言2. 问题3. 解决办法4. jar 包冲突总结1. 前言之所以写这篇文章是因为在使用Mav

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

MySQL CTE (Common Table Expressions)示例全解析

《MySQLCTE(CommonTableExpressions)示例全解析》MySQL8.0引入CTE,支持递归查询,可创建临时命名结果集,提升复杂查询的可读性与维护性,适用于层次结构数据处... 目录基本语法CTE 主要特点非递归 CTE简单 CTE 示例多 CTE 示例递归 CTE基本递归 CTE 结