ybr-nx / laravel-mariadb
为Laravel添加MariaDB JSON选择支持
1.0.20
2019-09-11 05:18 UTC
Requires
- illuminate/database: >=5.3
README
为Laravel添加MariaDB JSON支持。需要至少MariaDB 10.2.3(使用->json()迁移需要10.2.7)
安装
使用composer
$ composer require ybr-nx/laravel-mariadb
配置(仅限Laravel 5.3和5.4)
在config/app.php中包含MariaDBServiceProvider
'providers' => [ /* * Package Service Providers... */ YbrNX\MariaDB\MariaDBServiceProvider::class, ]
将数据库配置中的driver设置为mariadb
'defaultconnection' => [ 'driver' => 'mariadb',
新增功能
迁移
在迁移期间为json字段添加所需验证
$table->json('field') //CHECK (JSON_VALID(field)) $table->json('field')->nullable() //CHECK (field IS NULL OR JSON_VALID(field))
查询构建器
构建与MariaDB一起工作的json选择语句
$query->where('somejson->something->somethingelse', 2) DB::table('sometable')->select('sometable.somedata', 'sometable.somejson->somedata as somejsondata')
并且JSON_SET()在MariaDB中的表现与MySQL 5.7相同
DB::table('sometable')->where('somejson->somedata', $id)->update(['somejson->otherdata' => 'newvalue']);
注意 MariaDB < 10.2.8 中存在JSON_EXTRACT()行为函数的bug。该bug已在MariaDB 10.2.8中修复:https://jira.mariadb.org/browse/MDEV-12604
//works with string in MySQL & MariaDB 10.2.8 $query->where('somejson->something->somethingelse', 'somedata') //works with string in MariaDB < 10.2.8 $query->where('somejson->something->somethingelse', '"somedata"')