gpt4 book ai didi

laravel 5.6 id 自动转换为 int

转载 作者:行者123 更新时间:2023-12-05 00:48:36 25 4
gpt4 key购买 nike

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

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字段为整数:
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 [▶]
}

在我的模型中,我在没有任何运气的情况下添加了以下内容:
class Traffic extends Model {

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

从我到目前为止发现的情况来看,这应该是转换 id 的方法。输入字符串的字段。所以我要么做错了什么,要么这是一个错误。
关于如何解决这个问题的任何想法?

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

最佳答案

ZeroOne提及:

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



这对我有用。

关于laravel 5.6 id 自动转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49411532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com