The*_*uls 4 casting laravel laravel-5.6
我正在使用具有以下表结构的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();
})
Run Code Online (Sandbox Code Playgroud)
当我转储单个记录时,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 [?]
}
Run Code Online (Sandbox Code Playgroud)
在我的模型中,我添加了以下内容而没有任何运气:
class Traffic extends Model {
protected $casts = [
'id' => 'string'
];
Run Code Online (Sandbox Code Playgroud)
根据到目前为止的发现,这应该是将id字段强制转换为字符串类型的方法。所以我做错了什么,或者这是一个错误。关于如何解决这个问题有什么想法吗?
我知道如果创建一个整数字段id或将当前字段重命名为其他名称,则可能会解决。但我希望有更好的解决方案。
| 归档时间: |
|
| 查看次数: |
672 次 |
| 最近记录: |