Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mariaDB JSON support in Laravel

I'm trying to create a json database in XAMP, while using the phpmyAdmin it showed me that I'm using mariaDB but in my xamp-control panel v3.2.2 it shows running mySQL on port 3306. I'm using Laravel 5.4 framework to create the database, following is my migration which I'm trying to execute:

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();
});

Now while executing this I'm getting following error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json null, content json null, scripts json null, deleted_at timestamp null' at line 1 (SQL: create table pages (id int unsigned not null auto_increment primary key, title varchar(191) not null, slug varchar(191) not null, styles json null, content json null, scripts json null, deleted_at timestamp null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

Even if I keep not null it throws the same error. I want to have json formatted data, I checked the supported version and as per the documentation json format support started from the version MariaDB 10.0.16. and I'm using 10.1.21-MariaDB

Help me out in this.

like image 860
Nitish Kumar Avatar asked Feb 23 '17 20:02

Nitish Kumar


People also ask

Does MariaDB support JSON?

JSON is fast becoming the standard format for data interchange and for unstructured data, and MariaDB Platform (in fact, all MariaDB versions 10.2 and later) include a range of JSON supporting functions.

Can I use MariaDB with laravel?

Currently, Laravel provides first-party support for five databases: MariaDB 10.3+ (Version Policy) MySQL 5.7+ (Version Policy) PostgreSQL 10.0+ (Version Policy)

What is the use of JSON in laravel?

Laravel JSON is a small package that makes encoding and decoding JSON a breeze with exceptions thrown on error immediately: A simple wrapper around json_encode() and json_decode() for catching any errors without executing json_last_error() .

What is JSON data type in MySQL?

MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column: Automatic validation of JSON documents stored in JSON columns.


2 Answers

Since MariaDB version 10.2.7; theJSON data-type is an alias for LONGTEXT.

If you are having issues with the JSON data-type in MariaDB, simply just change it to LONGTEXT. ;-)

Or add MariaDB JSON to Laravel with this package

like image 130
ybr-nx Avatar answered Sep 30 '22 00:09

ybr-nx


Add MariaDB JSON support to Laravel by running this command using composer:

composer require ybr-nx/laravel-mariadb

If you are using Larvel 5.3 and 5.4 do these two items:

  • Include MariaDBServiceProvider in config/app.php by adding this line to providers:
'providers' => [
    // other exist providers
    YbrNX\MariaDB\MariaDBServiceProvider::class,
]
  • Set default connection in database configuration to mariadb:
'defaultconnection' => [
    'driver' => 'mariadb',

Adding package is done and then you can use functionalities.

In Migrations:

$table->json('field') //CHECK (JSON_VALID(field))
$table->json('field')->nullable() //CHECK (field IS NULL OR JSON_VALID(field))

For Query builder:

$query->where('somejson->something->somethingelse', 2)
DB::table('sometable')->select('sometable.somedata', 'sometable.somejson->somedata as somejsondata')

Also, JSON_SET() works in MariaDB as in MySQL 5.7:

DB::table('sometable')->where('somejson->somedata', $id)->update(['somejson->otherdata' => 'newvalue']);

Note 1: MariaDB has an alias for JSON datatype since version 10.2.7

Note 2: There is bug in MariaDB < 10.2.8 JSON_EXTRACT() behaviour function. It's fixed in MariaDB 10.2.8

retrieved from

like image 42
Pejman Kheyri Avatar answered Sep 30 '22 00:09

Pejman Kheyri