Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow adding custom casters and overriding laravel's own via config #39

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