Category: Uncategorized (page 1 of 2)

20 Eloquent tricks – more

Recently I stumbled upon interesting article by Povilas Korop on laravel-news 20 eloquent tips and tricks. First of all thanks for sharing your knowledge Povilas – it helps many people out there, keep it up!

As you probably know, I’ve worked with Eloquent for long and love its features, at the same time I’ve become very sensitive about some of the dangers lurking for inexperienced developers that come with all the magic of Eloquent :)
Let’s use this opportunity to look at the tips from different perspective and learn even more by going through some of them in more details!

Continue reading

A story about Laravel Resources

Leo is a brilliant developer. He turns every idea into code in no time. He’s prepared for any task thrown at him and dives into coding straight away, delivering successfully and rapidly.

These days he’s been working on a public API for the application he and his team is building. A few resources with a few endpoints were not a big challenge for Leo obviously. He caught some models in the wild and turned them into beatiful REST API data source. Done deal.

A few days passed and Whoops! Something went wrong unfortunately.

Continue reading

How to add custom casters in Laravel tinker

I made a series about psysh and Laravel’s tinker – powerful REPL where you can play with your whole application.

ICYMI check it out here: tinker like a boss and here’s example of what we can achieve:

There is, however, missing piece of the puzzle in the series. It describes how you can custimze presenters in REPL (called casters in PsySH), but I describe generic, global casters that reside in PsySH config. This is very handy, but you cannot distribute them among your team members (or anyone else for that matter) in the repo.

Today we are going to add that piece to the puzzle and see how to customize and override psysh casters in Laravel app.
Continue reading

Too much M4g1c will kill you (or at least bite your ass)


So, as you know, I’m a big fan of Laravel, in particular Eloquent is my favourite.
The beauty and value of both is a lot of conventions and magic happening behind the scenes. That magic, when balanced, allows us focus on things that really matter, rather than taking care of boilerplate and tedious, repetitive tasks, we’ve all been doing so many times. However…
Continue reading

Tinker like a boss – WTF?!

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?

Errors – WTF?

Usually getting verbose errors in tinker is now what you want. You made a typo, called wrong method etc – all feedback you need then is a short message and you can fix your error. Sometimes, however, you may want to actually see more information about the errors or full stacktrace behind it.
Continue reading

they can bite! – how (not) to use accessors in Eloquent


Eloquent mutators & accessors are handy feature that can simplify your high level code and hide some data-related logic in the model.

However, it is pretty common to misuse them and soon regret it.

Let’s take a look and see where we can go wrong…

Continue reading

PHP how to use $this in Closure – context matters


Anonymous functions, in php known as Closures, come in handy very often. One application in particular is very useful – extending classes, thanks to the capability to bind the closure to an instance and class scope.

However, binding to instance may prove a bit tricky, so let’s go through it together.

Continue reading

Revisionable – a quick example

A short reminder about my revisionable package (compatible with L4 & L5+) and quick example of how you can use it:


Continue reading

Laravel – Custom pivot model in Eloquent


Sometimes you may wish to use custom pivot model when working with many-to-many relationships in Eloquent, in order to add some behaviour on top of the core features.

There are a few things to consider before you start.

Continue reading

Laravel – querying any level far relations with simple trick


Eloquent provides one Relation type for far related tables – hasManyThrough. However it works only with with cascade of hasOne/hasMany relations and you can use it only for 2 levels of nesting, while sometimes you need more.

Imagine, that you want to get the posts for logged-in user, having this setup:

Now, because there is no built-in method for such case, you would probably think of getting all the tags, looping through them, and merging all the posts from each tag. While this would work, it would be cumbersome, so let’s make it easier!

Continue reading


Copyright © 2020 SOFTonSOFA

Theme by Anders NorenUp ↑