Time format in laravel migration?
You can use $table->time('field_name');
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('arena');
$table->time("begin");
$table->timestamps();
});
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();
});
And in your Post
model you can simply do this:
protected $casts = [
'begin' => 'date:hh:mm'
];
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 getBeginAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('hh:mm');
}
And every time that you echoing it out in your blade or wherever like this {{ $post->begin }}
it automatically changes the format for you.
Hope that helps.
You could do this with Carbon, which is included in Laravel by default:
- Set a variable with the current time
$date = Carbon::now()
- Set hour, for example 22
$date->hour = 22
- Set minutes, for example 54
$date->minutes = 54
- Finally, set
$table->date($date)