This is part of the series tinker like a boss (in psysh)

  1. tinker… what?
  2. includes
  3. casters (presenters)
  4. aliases
  5. http (and other) helpers
  6. wtf is this error?

Custom casters

Using Laravel, you’re already familiar with idea of casters even if you’re not aware of that. Check this out:

You can spot the difference – User model shows you some useful, customized info, whereas other classes will show only public properties (none for Validator factory).

To be precise Eloquent Models will output their array representation (that is visible attributes, loaded relations, appended accessors), and Laravel provides out-of-the-box casters for 3 classes Application, Model and Collection:

Now, what if we work often in tinker with particular class and would like to customize what it outputs?

I often play with DB related stuff – be it simply getting some data, or designing and optimizing complex queries, there are things that might come in handy. Let’s say I would like to see actual query that will be run:

Obviously repeating this for every query when I was working on optimizations and queries were far from this simple, I’d rather want it to be done for me:

How cool is that?!

Yeah, so here’s how easily you can do that in PsySH config ~/.config/psysh/config.php:

Take a look at config real-world example for details and create your own casters, that can help you with day-to-day tasks.

 

Enjoy and share your thoughts in the comments below or on twitter @SOFTonSOFA

Now is the time to go to your console, type $ php artisan tinker and follow along

Laravel – tinker like a boss with PsySH from Jarek on Vimeo.

It's only fair to share...Tweet about this on TwitterShare on RedditPin on PinterestShare on FacebookShare on Google+

Related Post

Tinker like a boss – aliases This is part of the series tinker like a boss (in psysh) tinker... what? includes casters (presenters) aliases http (and other) helpers ...
Tinker like a boss – custom includes This is part of the series tinker like a boss (in psysh) tinker... what? includes casters (presenters) aliases http (and other) helpers ...
Tinker like a boss (in psysh) tinker... what? includes casters (presenters) aliases http (and other) helpers wtf is this error? Tinker... what? REPL idea...
Querying relations in Laravel: get Models where la... Another part of Querying relations in Laravel will cover such problem: I want to get SomeModels that have latest RelatedModel (hasMany) matchin...