遇到了这样的问题。有一个 Booking 模型,它有 time_from 和 time_to 两个字段。当我调用某种 Booking 时,想要使时间格式不同。尝试使用 $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();
});
}
结果