Migration in Laravel

Viewed 1095

I am getting an error while migrating my tables. the error says

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, start_date date not null, end_date date not null, status enum('' at line 1 (SQL: create table modules (id int unsigned not null auto_increment primary key, title varchar(191) not null, description text not null, image blob null, resources json null, start_date date not null, end_date date not null, status enum('pending', 'start', 'completed') not null default 'pending', user_id int not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

my table have following values:

public function up()
    {
        Schema::create('modules', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->binary('image')->nullable();
            $table->json('resources')->nullable();
            $table->date('start_date');
            $table->date('end_date');
           $table->ENUM('status',['pending','start','completed'])->default('pending');
            $table->integer('user_id');
            $table->timestamps();
        });
    }

this is my model code:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

    class modules extends Model
    {
        protected $fillable = [
            'title', 'description','image','resources', 'start_date','end_date','status','user_id',
        ];
    }

can anyone have the solution for this??

3 Answers

As of Laravel, the $table->json() method will try to create an actual JSON field in the database. However, the JSON field was not added to MySQL until MySQL 5.7.8.

Therefore, if you're using a version of MySQL previous to 5.7.8, you need to just create it as a text field

MariaDB new version supported JSON. (Alpha version. Not recommended by Maria to production server. Only testing.)

MariaDB 10.1 do not support JSON

May be adding the $casts class property will fix your problem.

class modules extends Model
{
    protected $casts = [
        'resources' => 'array',
    ];
}

Here is something that may help you.

Related