laravel数据库模型蛇形命名自动转换驼峰命名

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;

以下是注意的几点:

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": "c3284d0f9",
		"salt": "",
		"sex": 3,
		"email": "",
		"mobile": "",
		"loginIp": "",
		"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转化成驼峰

class Feedback extends Model
{
    use CamelCasing;

    protected $table = 'feedback';
    protected $primaryKey = 'id';
    protected $guarded = [];

    public $timestamps = false;


    public function getNickNameAttribute($value)
    {
        return !empty($value) ? json_decode($value, true) : '';
    }

    public function setNickNameAttribute($value)
    {
        $this->attributes['nick_name'] = !empty($value) ? json_encode($value) : '';
    }
}

5,一些基础问题说明

1,为什么要用驼峰命名数据库,这个主要是个人习惯,但是类名,方法名,变量名laravel中大多数是驼峰
2,全部使用蛇形命名有什么问题吗?答:没问题
3,全部使用驼峰命名有什么问题吗?答:没问题
4,为什么要有这个命名转化为驼峰命名的插件呢?其实为了代码风格统一,方便开发习惯

你可能感兴趣的:(laravel,数据库,数学建模,php)