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?
JavaScript
x
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.
Advertisement
Answer
In laravel 5.6 you have this new feature that you can cast your timestamp so your migration should be like this
JavaScript
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:
JavaScript
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
JavaScript
public function getBeginAttribute($date)
{
return CarbonCarbon::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.