1
0

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】DBファサードで取得した日付にformatメソッドを使うとエラーになる(Call to a member function format() on string)

Posted at

はじめに

DBファサードを用いて、timestamp型のcreated_atの値をview側でformat('Y年m月d日 H時i分')をかけて表示しようとしたところ、
「Call to a member function format() on string」
というエラーが発生しました。

対処法

DBファサードを利用することで、timestamp型がstring型になっていることが分かったので、
phpのstrtotime()を使ってフォーマットをかけると上手くいきました!

viewファイル
<p>
   作成日:{{ date('Y年m月d日 H時i分' ,strtotime($properties->created_at)) }}
</p>
コントローラー
<?php
namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class PropertyController extends Controller
{
    public function index()
    {
            return view('admin.index', [
                'properties' => DB::table('properties')->paginate(10)
            ]);
    }
}

終わりに

以上、自分用備忘録でした。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?