Skip to content

Conversation

@jarektkaczyk
Copy link

Hi!

This one allows adding custom and/or overriding laravel's own PsySH casters via array provided in config('tinker.casters')

=> Carbon\Carbon @1519569240 {#745
     +date: "2018-02-25 14:34:00.000000",
     +weekday: "Sunday",
     +day_of_year: 55,
     +unix: 1519569240,
     +diff_for_humans: "6 days ago",
   }
Setup
// config/tinker.php
return [
    'casters' => [
        // ordinary class-based method:
        Carbon\Carbon::class => 'App\Console\Casters::carbon',

        // or inline:
        Carbon\Carbon::class => function (Carbon\Carbon $carbon) {
            return [
                'date' => $carbon->date,
                'weekday' => $carbon->format('l'),
                'day_of_year' => (int) $carbon->format('z'),
                'unix' => (int) $carbon->format('U'),
                'diff_for_humans' => $carbon->diffForHumans(),
            ];
        },
    ],
];
namespace App\Console;

class Casters
{
    public static function carbon(Carbon\Carbon $carbon)
    {
        return [
            'date' => $carbon->date,
            'weekday' => $carbon->format('l'),
            'day_of_year' => (int) $carbon->format('z'),
            'unix' => (int) $carbon->format('U'),
            'diff_for_humans' => $carbon->diffForHumans(),
        ];
    }
}

@taylorotwell
Copy link
Member

No plans on adding this right now.

@jarektkaczyk
Copy link
Author

jarektkaczyk commented Mar 5, 2018 via email

@zachdecook
Copy link

Implemented by #133 in late 2021 (v2.6.2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants