当使用laravel 5.2获取错误从mysql 5.7中的json列类型检索json数据时

时间:2016-03-22 12:16:05

标签: json laravel-5.2 php-5.6 mysql-5.7

我在我的应用程序中使用mysql 5.7,laravel5.2和php 5.6.17。

我创建了一个迁移表

   public function up()
    {
        Schema::create('admin_login_history', function (Blueprint $table) {
            $table->increments('id');
            $table->string('url');
            $table->json('client_details');
            $table->integer('user_id')->nullable();
            $table->timestamps();
        });
    }

我为它创建了一个模型..

class AdminLoginHistory extends Model
{

    protected $table = 'admin_login_history';

    protected $fillable = ['file_path', 'url', 'user_id', 'client_details'];

    protected $casts = ['client_details' => 'json'];
}

我插入了一个client_details数据数组,在我的模型中转换为json。

现在从表中检索数据时出现错误

$loginhistory = AdminLoginHistory::where('user_id', $id)->orderBy('created_at', 'desc')->take(3)->get();

错误:

File Path: /home/storage/elivio/InvizoAdmin/vendor/laravel/framework/src/Illuminate/Database/Connection.php
Error Code:HY000,  Line Number:669
Message:SQLSTATE[HY000]: General error: 2036 (SQL: select * from `admin_login_history` where `user_id` = 1 order by `created_at` desc limit 3)

2 个答案:

答案 0 :(得分:0)

这可能类似于this post。 JSON数据在MySQL中仍然相对较新,所以我想在事情变得坚实之前需要花一点时间。

答案 1 :(得分:0)

“从MySQL 5.7.8开始,MySQL支持本机JSON数据类型......”因此,请检查您是否具有正确的mysql版本(5.7.8或更高版本)。

Mysql 5.7.8 JSON data type, doc