2023年6月20日15:10:59
我看了各种方案,但是多多少少都有各种问题
建议使用 https://github.com/kirkbushell/eloquence
安装
composer require kirkbushell/eloquence
添加到 provider
添加 eloquence service provider 在你的 config/app.php
文件中
'providers' => [
/*
* Application Service Providers...
*/
Eloquence\EloquenceServiceProvider::class,
],
使用方法
在模型添加 use \Eloquence\Behaviours\CamelCasing;
<?php
namespace App\Models;
use Eloquence\Behaviours\CamelCasing;
use Illuminate\Database\Eloquent\Model;
class Feedback
{
use CamelCasing;
protected $table = 'feedback';
protected $primaryKey = 'id';
protected $guarded = [];
public $timestamps = false;
}
以下是注意的几点:
1,add save的时候你的模型的对象属性是蛇形命名还是驼峰命名都是可以,因为在包里调用的
Str::snake($key)
方法,如果是蛇形命名就不会改变,是驼峰就会转换成蛇形,例如:
isset($where['nickName']) && $feedback->nickName = $where['nickName'];
$res = $feedback->save();
数据库的字段是:nick_name
2,返回数据的时候,会自动驼峰转换
{
"code": 200,
"msg": "操作成功",
"data": {
"count": 2,
"list": [
{
"id": 13,
"nickName": "33333",
"contact": "11111",
"content": "2222",
"createAt": "2023-06-21 09:58:23",
"updateAt": "2023-06-21 09:58:23",
"platform": "PC",
"lang": "zh-CN"
}
]
}
}
数据库的结果:
CREATE TABLE `feedback` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`nick_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '反馈昵称',
`contact` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '联系方式',
`content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '反馈内容',
`create_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`platform` varchar(10) COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'PC' COMMENT '平台类型',
`lang` varchar(10) COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'zh-CN' COMMENT '语言类型',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='反馈表';
3,联表测试返回的数据
$banner = Banner::with(['admin_zx']);
class Banner extends Model
{
use CamelCasing;
protected $table = 'banner';
protected $primaryKey = 'id';
protected $guarded = [];
public $timestamps = false;
public function admin_zx()
{
return $this->hasOne(Admin::class, 'id', 'admin_id');
}
}
返回数据:
{
"id": 37,
"name": "百亿",
"adminId": 5,
"url": "l",
"sort": 11,
"startTime": null,
"endTime": null,
"createAt": "2022-01-21 15:50:50",
"updateAt": "2022-01-23 22:52:56",
"picPath": "/upload/image/20190322/57da8821d96f8391b084757a8d41e072.jpg",
"videoPath": null,
"platform": "10",
"lang": "zh-CN",
"adminZx": {
"id": 5,
"name": "admin",
"password": "c3284d0f94606de1fd2af172aba15bf3",
"salt": "",
"sex": 3,
"email": "",
"mobile": "",
"loginIp": "113.110.200.178",
"status": 1,
"avatar": null,
"realName": "admin",
"loginTime": "2023-03-21 08:31:03",
"adminGroupIds": "9",
"isAdmin": 1,
"sort": 255,
"createAt": "2022-01-21 15:45:09",
"updateAt": "2023-03-21 08:31:03"
}
},
4,使用属性自定义处理,不会影响返回nick_name
转化成驼峰
public function getNickNameAttribute($value)
{
return !empty($value) ? json_decode($value, true) : '';
}
public function setNickNameAttribute($value)
{
$this->attributes['nick_name'] = !empty($value) ? json_encode($value) : '';
}
标签:laravel,COMMENT,21,utf8mb4,DEFAULT,蛇形,命名,NULL,id
From: https://www.cnblogs.com/zx-admin/p/17493699.html