Reputation: 971
I would like to know how to recuperate the format time on my form please? For example start_time => 20:00 and end_time 22:00.
In my table course I have this
public function up()
{
Schema::create('course', function (Blueprint $table) {
$table->increments('id');
$table->date('date_seance');
$table->time('start_time');
$table->time('end_time');
$table->timestamps();
});
}
Then, on my model course I have this
class Course extends Model
{
//
protected $dates = ['date_seance', 'start_time', 'end_time'];
}
In my view index.blade
@foreach($course as $item)
<tr>
<td> {{$item->date_seance->format('d/m/Y') }}</td>
<td> {{$item->start_time}}</td>
<td> {{$item->end_time}}</td>
Thank you for your help.
Upvotes: 2
Views: 3344
Reputation: 31
I know it's old but why not try a simple cast on model? Something like:
class Course extends Model
{
protected $casts = [
'date_seance' => 'datetime:d/m/Y',
'start_time' => 'datetime:H:i',
'toend_time' => 'datetime:H:i',
];
}
Check the docs here if you need more cast's types: https://laravel.com/docs/10.x/eloquent-mutators#attribute-casting
Upvotes: 0
Reputation: 5386
Use Mutators and Accessors.
setStartTimeAttribute
to save time in databaseAn Accessor getStartTimeAttribute
to show it with required format i.e. h:i or H:i
public function setStartDateAttribute($value)
{
$this->attributes['start_time'] = Carbon::parse($value)->format('H:i');
}
public function getStartDateAttribute()
{
return Carbon::parse($this->attributes['start_time'])->format('H:i');
}
Now you can access formatted time as
$object->start_time
i.e. 20:00
Upvotes: 2
Reputation: 6058
I don't think you can use 'start_time'
and 'end_time'
in date since they are not.
class Course extends Model
{
protected $dates = ['date_seance'];
}
Then use
<td> {{$item->date_seance->format('d/m/Y') }}</td>
<td> {{date('H:i', strtotime($item->start_time)) }}</td>
<td> {{date('H:i', strtotime($item->end_time)) }}</td>
Upvotes: 0
Reputation: 257
try below format you already stored in time so no need to convert in strtotime
<td> {{date('H:i', $item->start_time) }}</td>
Upvotes: 0
Reputation: 21681
You should try this:
<td> {{date('d/m/Y H:i', strtotime($item->date_seance)) }}</td>
Upvotes: -1
Reputation: 1556
the type of column is instance of Carbon class what about trying this :
<td> {{$item->date_seance->toDateString() }}</td>
Upvotes: 0