現在日時と比較して「○分前」と表示させたい
CarbonライブラリにあるdiffForHumans()メソッドを使えばOK
$ php artisan tinker Psy Shell v0.10.4 (PHP 7.4.10 — cli) by Justin Hileman >>> now()->diffForHumans(now()->subDays(2)->subMinutes(2)); => "2日後" >>> now()->subDays(2)->subMinutes(2)->diffForHumans(); => "2日前" >>> now()->subMinutes(2)->diffForHumans(); => "2分前"
now()->subDays(2)->subMinutes(2) のところをEloquentにしてあげれば
以下のような書き方でbladeファイル内で「○分前」と表示させることができます。
$notification->created_at->diffForHumans();
ちなみにこの書き方のみだと3年前、10年前とも表示されてしまうので
ある一定期間より前の投稿なら日付を表示させる処理は以下になります。
<div> @if ($notification->created_at->diffInDays(now()) <= 3) {{ $notification->created_at->diffForHumans() }} @else {{ $notification->created_at->isoFormat("YYYY/MM/DD") }} @endif </div>