laravel 5.6 id自动转换为int [英] laravel 5.6 id is automatically casted to int

查看:89
本文介绍了laravel 5.6 id自动转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用具有以下表结构的Laravel 5.6:

I'm using Laravel 5.6 with the following table structure:

public function up() {
        Schema::create( 'traffic', function ( Blueprint $table ) {
            $table->string( 'id' );
            $table->unsignedInteger( 'category_id' );
            $table->unsignedInteger( 'magnitude_id' );
            $table->unsignedInteger( 'start_id' )->nullable();
            $table->unsignedInteger( 'destination_id' )->nullable();
            $table->unsignedInteger('locale_id');
            $table->integer( 'length' )->comment( 'in metres' );
            $table->integer( 'delay' )->comment( 'in seconds' );
            $table->decimal( 'position_lat', 10, 8 );
            $table->decimal( 'position_lon', 10, 8 );
            $table->timestamps();
})

当我转储单个记录时,laravel转换了 id 字段转换为整数:

When I dump a single record, laravel has converted the id field to an integer:

Traffic {#351 ▼
  #casts: array:1 [▶]
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: false
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:13 [▼
    "id" => 3
    "category_id" => 6
    "magnitude_id" => 3
    "start_id" => 1898
    "destination_id" => 1898
    "locale_id" => 3
    "length" => 3650
    "delay" => 3061
    "position_lat" => "5.27669000"
    "position_lon" => "51.85458000"
    "created_at" => "2018-03-21 15:55:13"
    "updated_at" => "2018-03-21 15:55:13"
    "name" => "nl"
  ]
  #original: array:13 [▶]
  #changes: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:10 [▶]
  #guarded: array:1 [▶]
}

在我的模型中,我添加了以下内容而没有任何运气:

In my model I have added the following without any luck:

class Traffic extends Model {

    protected $casts = [
        'id' => 'string'
    ];

根据到目前为止的发现,这应该是投射 id 字段以键入字符串。所以我做错了什么,或者这是一个错误。
关于如何解决这个问题的任何想法?

From what I've found so far this should be the way to cast the id field to type string. So I'm either doing something wrong or this is a bug. Any ideas on how I could solve this?

我知道如果创建整数字段 id可能会解决所有问题或将当前字段重命名为其他名称。但我希望有更好的解决方案。

I know it wold probably be solved if I created an integer field id or rename the current field to something else. But I'm hoping there is a better solution.

推荐答案

ZeroOne 提到:


您是否已将此公共$ incrementing = false; ..需要禁用我
认为

have you add this public $incrementing = false;.. need to be disable i think

这对我有用。

这篇关于laravel 5.6 id自动转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆