laravel迁移中的时间格式? [英] Time format in laravel migration?
问题描述
我想输入一个信息,您以欧盟格式(如12:00或21:34)放置时间. (hh:mm) 我该怎么办?
I want to have an input, where you put a time in EU format like 12:00 or 21:34. (hh:mm) How do I do that?
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('arena');
$table->date("h,i"('beginn'));
$table->timestamps();
});
这就是我所拥有的,但显然是错误的.
This is what I have, but it's obviously wrong.
推荐答案
在laravel 5.6中,您具有此新功能,可以转换时间戳记,以便迁移应像这样
In laravel 5.6 you have this new feature that you can cast your timestamp so your migration should be like this
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('arena');
$table->timestamp("begin");
$table->timestamps();
});
在您的Post
模型中,您可以简单地执行以下操作:
And in your Post
model you can simply do this:
protected $casts = [
'begin' => 'date:hh:mm'
];
修改
如果您未使用laravel 5.6,则可以使用访问器和&突变器,可以轻松地在设置数据库或从数据库中获取数据时进行操作,因此您可以执行类似的操作
Edit
If you are NOT using laravel 5.6 you can use Accessors & Mutators to easily manipulate data on setting on database or getting it from database so you can do something like this
public function getBeginAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('hh:mm');
}
每当您在刀片中或类似{{ $post->begin }}
这样的地方将其回显时,它都会自动为您更改格式.
And every time that you echoing it out in your blade or wherever like this {{ $post->begin }}
it automatically changes the format for you.
希望有帮助.
这篇关于laravel迁移中的时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!