Cara Atasi Error Migrasi Table Bouncer di Laravel 5.8

Bagi pengguna Laravel, mengatur hak akses pengguna atau memberikan akses atas kemampuan tertentu pada pengguna, entah sebagai administrator, contributor, webmaster, atau user biasa adalah hal yang harus dibuat dalam aplikasi multiuser. Salah satu package untuk mengatur role dan abiliy user di Laravel adalah Bouncer.



Jika anda baru saja melompat misal dari 5.4 ke 5.8 --atau bagi anda pengguna XAMPP-- Anda akan menemui masalah error saat melakukan langkah migrate table Bouncer.


Illuminate\Database\QueryException  : 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, `scope` int null, `created_at` timestamp null, `updated_at` timestamp' at line 1 (SQL: create table `abilities` (`id` int unsigned not null auto_increment primary key, `name` varchar(191) not null, `title` varchar(191) null, `entity_id` int unsigned null, `entity_type` varchar(191) null, `only_owned` tinyint(1) not null default '0', `options` json null, `scope` int null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

at D:\project\projectname\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("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, `scope` int null, `created_at` timestamp null, `updated_at` timestamp' at line 1")      D:\project\redtenvi-laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452

  2   PDO::prepare("create table `abilities` (`id` int unsigned not null auto_increment primary key, `name` varchar(191) not null, `title` varchar(191) null, `entity_id` int unsigned null, `entity_type` varchar(191) null, `only_owned` tinyint(1) not null default '0', `options` json null, `scope` int null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'")
      D:\project\projectname\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452

  Please use the argument -v to see more details.

Permasalahan di atas terjadi karena versi MariaDB yang digunakan tidak mendukung penggunaan tipe data json pada kolom options di table abilities. Minimal anda harus memakai versi MariaDB 10.2.

Solusinya anda harus mengupgrade MariaDB anda dengan versi terbaru, atau jika anda pengguna XAMPP maka upgrade XAMPP anda. Solusi lainnya jika anda tidak ingin mengupgrade (meski itu solusi yang terbaik) adalah dengan mengubah tipe data json menjadi string.

$table->json('options')->nullable();

Menjadi

$table->string('options')->nullable();

Demikian semoga bermanfaat.