In this episode, we dive into Eloquent's query
method in Laravel and explore some different ways you can use it in your projects. We start by spinning up a brand new Laravel app and make a simple Post
model with a couple of records to play with. After a quick overview of how you'd normally grab a post—for example, finding one by its slug using the where
method—we look at how chaining with the query
method actually works.
The video breaks down what's happening under the hood when you use methods like where
directly on your model versus when you use them after calling query()
. We dig into the Laravel source code a bit to see how calls get magically forwarded to the query builder behind the scenes and why that's possible.
By the end, you'll understand the basic mechanics of querying models in Laravel and get a sneak peek of why reaching for the query()
method can sometimes be not just syntactic sugar but actually helpful. In the next episode, we'll start to see the practical reasons you might want to use query()
—so stay tuned!