Using Eloquent subqueries to randomise grouped results →
Selecting random records from grouped data isn't as simple as it seems. Here's how to do it in Laravel, using Eloquent subqueries.
Laravel is a batteries-included PHP framework based on the Model-View-Controller (MVC) paradigm. Out of the box, Laravel includes features most modern web development projects need, such as authentication, validation, and database queries.
High-quality first and third-party packages such as Nova and Livewire build on this solid base to ensure that Laravel developers rarely need to create a custom solution from scratch.
The following articles delve into the day-to-day realities of working with Laravel and its many nuances and foibles. If you’ve already mastered the basics, they’re a great way to dig a little deeper.
Selecting random records from grouped data isn't as simple as it seems. Here's how to do it in Laravel, using Eloquent subqueries.
Going into this project, I assumed the biggest problem would be how to retrieve the status of each product. As ever, the real problem was naming things.
The plan has always been to launch Happy Stack with email notifications; that's it. I have lots of other notification channels planned of course, but email is still the most universal.
Nova uses Laravel policies to manage resource authorisation. This can be problematic when it comes to resource actions.
How to conditionally apply Laravel validation rules, even when you don’t have access to a validator instance.
When you define a resource, Laravel Nova makes an educated guess regarding its URL slug. You can override the default slug by implementing the uriKey
method. Here's how.
Laravel makes heavy use of the Macroable
trait throughout its codebase, but the official docs only mention it in passing. Let's dig in.
Route model binding is a very useful Laravel feature. But what happens if you have an endpoint which needs to support multiple, comma-delimited IDs? Route collection binding to the rescue.
The standard method for removing a global scope from an Eloquent model is a little clunky. We can do better.
I recently found myself with the need to add a custom method to the LengthAwarePaginator
class. Not a problem, I thought, I'll write a quick macro. A fine solution, but for the fact the LengthAwarePaginator
isn't "macroable". Or so it would seem at first glance.
A simple, performant solution for eager-loading Eloquent relationships, when you're only interested in one or two specific properties on the related model.
Eloquent makes it easy to respond to certain key points in the lifecycle of a model instance, by exposing events such as creating
, updating
, and deleted
.
There's an important gotcha to remember when working with Eloquent and default database values: if you create a new model instance, without overriding a default value, the attribute will not be set.
A monthly round-up of blog posts, projects, and internet oddments.