【发布时间】:2021-01-02 14:51:31
【问题描述】:
遇到了这样的问题。有一个预订模型。预订有字段 time_from 和 time_to。当我调用某种预订时,希望格式有所不同。尝试使用 $casts = ['time_from' => 'datetime:m-d-Y'];不工作!可能是什么问题???
型号
class Booking extends Model
{
use HasFactory;
protected $casts = [
'time_from' => 'datetime:m-d-Y',
'time_to' => 'datetime:m-d-Y'
];
protected $dateFormat = 'm-d-y';
protected $fillable = [
'room_id',
'time_from',
'time_to',
'first_name',
'last_name',
'phone',
'email',
'special_requirements',
'when_wait_you',
];
public function room(){
return $this->belongsTo(Room::class);
}
}
迁移
public function up()
{
Schema::create('bookings', function (Blueprint $table) {
$table->id();
$table->bigInteger('room_id');
$table->dateTime('time_from');
$table->dateTime('time_to');
$table->string('first_name');
$table->string('last_name');
$table->string('phone');
$table->string('email');
$table->text('special_requirements')->nullable();
$table->string('when_wait_you')->nullable();
$table->timestamps();
});
}
结果
【问题讨论】:
-
什么“不工作”?.....
-
time_from 和 time_to 格式不是 m-d-Y。投射不工作
-
好的,你在哪里序列化模型?否则您无法确认它是否“工作”
-
我首先通过控制器进行预订,但属性 time_from 和 time_to 不起作用转换格式。
-
你怎么知道他们“不工作”?模型序列化了吗?
标签: laravel