Move Model Scopes To Traits In Laravel
– medium.com - submitted by CODE AXION
I have described where to put your scopes if your model is cluttered with 10 + scopes
Read more [medium.com]
Posts tagged with traits
– medium.com - submitted by CODE AXION
I have described where to put your scopes if your model is cluttered with 10 + scopes
Read more [medium.com]
– aschmelyun.com - submitted by Andrew Schmelyun
If you've been working with PHP regularly, chances are you've run across an Interface, Trait, or Abstract Class. At first glance, they might appear to have a few similarities between them, and it can be hard to make out their differences and use cases.
Read more [aschmelyun.com]
Join 9,500+ smart developers
Every month I share what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
PHP is a wonderful dynamic language that's capable of many cool things. I recently stumbled upon something quite fantastic that I want to share with you.
– doeken.org - submitted by Doeke Norg
Context from traits is copy-pasted to your class and NOT inherited. What does that imply?
Read more [doeken.org]
Traits are a wonderful thing in PHP. You can use them to reduce code duplication by putting common functions in a trait and apply them to all classes where those functions are needed. I also sometimes use traits to break up a large function in multiple single-use traits.
In this post, I'd like to show you how you can override a trait function and call it from the overriding function.
Jason McCreary makes the case for using traits over reaching for inheritance.
Read more [jasonmccreary.me]
Recently Laravel introduced a Tappable trait. This blogpost explains what this trait allows you to do.
As you might know, the Laravel framework comes with a handy tap method which allows you to call the given closure with the given value and then return the value. Sounds confusing? It probably is until you've seen it!
Read more [protone.media]
Neat little Laravel internal tip: When you want one of your classes to forward method calls to a different class, you can use the "ForwardsCalls" trait. The trait takes care of forwarding the method calls as well as catching possible exceptions. pic.twitter.com/F4dGypBNk7
— Marcel Pociot (@marcelpociot) January 23, 2019
Read more [twitter.com]
I love this little “owns” trait/method that @michaeldyrynda & I came up with for Confomo.
— Matt Stauffer (@stauffermatt) July 7, 2017
Look at that expressiveness. :kisses fingers: pic.twitter.com/dE731Ko1Zw
On the Tighten co blog Caleb Porzio wrote a small post on how you can boot your model traits. It's a very handy feature that's being used a quite a few Spatie packages.
TL;DR Use bootNameOfTrait() instead of boot() in a model trait to avoid being overwritten by the base model’s boot() method.
Nicola Malizia wrote a short blog post on how Laravel's handy Macroable trait can be used and how it works under the hood.
If you check the Laravel codebase I’m sure that you can observe that Laravel makes use of traits.There is one trait in the source code that pulls my attention. I’m talking about the Macroable trait. ... The purpose of this trait is to extend (not in an OOP sense) a class at run-time. This way, you can add behavior without editing the original class source code.
https://unnikked.ga/understanding-the-laravel-macroable-trait-dab051f09172
PHP does not support method overloading. In case you've never heard of method overloading, it means that the language can pick a method based on which parameters you're using to call it. This is possible in many other programming languages like Java, C++.
So, under normal circumstances, you can't do this in PHP:
class Foo
{
function bar(A $baz)
{
...
}
function bar(B $baz)
{
...
}
}
However, with some clever coding, Adam Wathan made a trait, aptly called Overloadable, that makes method overloading possible. It works by just accepting any parameters using the splat operator and then determining which of the given functions must be called according to the given parameters.
Let's rewrite that example above using the Overloadable trait.
class Foo
{
use Overloadable;
function bar(...$arguments)
{
return $this->overload($arguments, [
function (A $baz) {
$this->functionThatProcessesObjectA($baz);
},
function (B $baz) {
$this->functionThatProcessesObjectB($baz);
},
]);
}
}
Pretty cool stuff. In a gist on GitHub Adam shares a couple of examples, the source code of the trait and the tests that go along with it. Check it out!
https://gist.github.com/adamwathan/120f5acb69ba84e3fa911437242796c3
Mark Baker tried creating an Anonymous Class Factory.
I was intrigued by a request to find a way of dynamically applying Traits to a class at run-time. With time on my hands as I was sitting in the airport, I considered the problem; and my first thought was to build an Anonymous class, extending the requested class (so that it would still contain all the base properties and functionality, and could be type-hinted, but also applying the requested Trait or set of Traits.
https://markbakeruk.net/2016/05/03/in-search-of-an-anonymous-class-factory/
Edd Mann demonstrates a nice trait to add OrElse functionality to a class via a trait. With it you can do things like:
[code]
$cart = $repository->findByIdOrElse(1, new ShoppingCart); $cart = $repository->findByIdOrElse(1, function () { return new ShoppingCart; });
<a href="http://tech.mybuilder.com/optional-value-control-flows-in-php-using-traits-and-magic-methods/">http://tech.mybuilder.com/optional-value-control-flows-in-php-using-traits-and-magic-methods/</a>