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…
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…
Another part of Querying relations in Laravel will cover such problem:
I want to get SomeModels that have latest RelatedModel (hasMany
) matching.. _SOME CONSTRAINT HERE_
Eloquent provides expressive syntax for querying relations that can be used for simple cases: SomeModel::whereHas('relatedModels', function ($q) { ... })->get()
. Unfortunately it is not capable of handling the latest RelatedModel part just like that.
Let’s dive into how we can tackle this problem using 2 approaches:
Collection
methodssubqueries
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.
If you wrote some PHP, most likely you have used array_merge
here and there for your arrays. You may have met array_replace
function, introduced in PHP5.3. Finally, you could notice +
operator (aka union).
Since all 3 do similar thing, and the docs don’t quite describe the difference between them, here’s a nice image of it
A short reminder about my revisionable package (compatible with L4 & L5+) and quick example of how you can use it:
In this first post of a series about https://github.com/jarektkaczyk/eloquence – the package that allows working with eloquent models in even simpler manner – I’m going to introduce you to the searchable
feature of the builder class.
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.
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:
1 2 3 4 5 6 7 |
// User subscribed to many tags User belongsToMany Tag // Post migh be tagged by many tags Tag belongsToMany Post |
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!
After spending long time with Eloquent I finally decided to release some of my work – extensions that will make your life easier, when working with Eloquent ORM.
They will be released bit by bit on github and much more will be found in the book I’m working on.
https://leanpub.com/eloquence-emerging
As a starter take a look at the repo and make sure to follow, for I will be updating it regularly!
https://github.com/jarektkaczyk/eloquence
Leave your thoughts in the comments – what would you like me to cover in the book, what is the most difficult part that you need help with, what use-cases would be most desirable.
If you are looking for an example of Eloquent Global Scope feature, then you’re in the right place!
Check the ready-to-use demo at …
Copyright © 2019 SOFTonSOFA
Theme by Anders Noren — Up ↑