![](/img/trans.png)
[英]Where JSON in Laravel gives error SQLSTATE[42000]: check the manual that corresponds to your MariaDB server
[英]mariaDB JSON support in Laravel
我正在嘗試在 XAMP 中創建一個 json 數據庫,同時使用 phpmyAdmin 它顯示我正在使用 mariaDB 但在我的xamp-control panel v3.2.2
它顯示mySQL on port 3306
運行mySQL on port 3306
。 我正在使用 Laravel 5.4 框架來創建數據庫,以下是我嘗試執行的遷移:
Schema::connection('newPortal')->create('pages', function (Blueprint $table){
$table->increments('id');
$table->string('title');
$table->string('slug')->unique()->index();
$table->json('styles')->nullable();
$table->json('content')->nullable();
$table->json('scripts')->nullable();
$table->softDeletes();
$table->timestamps();
});
現在,在執行此操作時,我收到以下錯誤:
SQLSTATE[42000]:語法錯誤或訪問沖突:1064 你的 SQL 語法有錯誤; 檢查與您的 MariaDB 服務器版本相對應的手冊,以在第 1 行(SQL:創建表
pages
(id
int unsigned not null auto_increment primary)附近的“json null、content
json null、scripts
json null、deleted_at
timestamp null”附近使用正確的語法鍵,title
varchar(191) 不為 null,slug
varchar(191) 不為 null,styles
json null,content
json null,scripts
json null,deleted_at
時間戳 null,created_at
時間戳 null,updated_at
時間戳 null) 默認字符集 utf8mb4 整理 utf8mb4_unicode_ci)
即使我保持不為空,它也會引發相同的錯誤。 我想要 json 格式的數據,我檢查了支持的版本,並根據從MariaDB 10.0.16.
版本開始的文檔 json 格式支持MariaDB 10.0.16.
我正在使用10.1.21-MariaDB
幫我解決這個問題。
MariaDB 自 10.2.7 版起具有 JSON 數據類型的別名
使用這個包將 MariaDB JSON 添加到 Laravel
請注意,1064 抱怨數據類型“json”。 這在 MariaDB 中(尚未)實現。
您可以接近Dynamic Columns ,它至少有一種方法可以將它們提取到 JSON 語法中。
另一件事(可能是您所指的)是CONNECT
能夠擁有 JSON表類型。 (不是列類型。)
MySQL 5.7 有一個名為JSON
的數據類型,加上一堆函數來操作它。
想出了一個簡單的解決方法(不推薦用於生產)-
根據 mariadb 10.1.32 及更低版本,似乎 mariadb 不支持 json 數據類型,我仍然不確定它是否在 10.2.7+ 版本中可用。
但這里有一個簡單的解決方法來解決這個問題。
將 json 數據類型更改為文本,然后再次運行遷移。
( https://user-images.githubusercontent.com/27993070/41234555-19c5d1d8-6dbf-11e8-9a4b-0644b03aecfc.png )
通過使用 composer 運行以下命令,為 Laravel 添加 MariaDB JSON 支持:
composer require ybr-nx/laravel-mariadb
如果您使用的是Larvel 5.3 和 5.4,請執行以下兩項:
config/app.php
包含 MariaDBServiceProvider,方法是將這一行添加到 providers 中:'providers' => [
// other exist providers
YbrNX\MariaDB\MariaDBServiceProvider::class,
]
'defaultconnection' => [
'driver' => 'mariadb',
添加包完成,然后您可以使用功能。
在遷移中:
$table->json('field') //CHECK (JSON_VALID(field))
$table->json('field')->nullable() //CHECK (field IS NULL OR JSON_VALID(field))
對於查詢構建器:
$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']);
注 1:MariaDB 自 10.2.7 版起就有了 JSON 數據類型的別名
注 2:MariaDB < 10.2.8 JSON_EXTRACT() 行為函數中存在錯誤。 它已在 MariaDB 10.2.8 中修復
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.