22
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel Viewでの日付フォーマットの変更

Last updated at Posted at 2018-03-12

###やりたいこと

  • "2000-02-02 00:00:00"を"2000/02/02"と表示したい

###実装

  • モデルファイルに$datesプロパティを記述する
    (created_at, updated_atは記述する必要なし)
  • 変更したい日付にformat()をつける

format()のパラメータ文字列は下記リンク参照
PHP:date - Manual

Model.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    protected $guarded = ['id'];
    protected $dates = ['display_date'];
}
blade.php

    @foreach($items as $item)
-     <div>{{ $item['display_date'] }}</div> //2000-02-02 00:00:00
+     <div>{{ $item['display_date']->format('Y/m/d') }}</div> //2000/02/02
    @endforeach
22
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?