Laravel's real-time facades feel like magic - in the best possible way. You try it out, it works, and you think to yourself, "Huh...how on earth did they do that?" Well, I'll show you exactly how it works. We'll even make our own custom facade generator for learning purposes.
PHP generators can be confusing to understand. Why and when would I reach for them? The easiest answer to this question is when you're working with very large datasets. A generator allows us to circumvent memory limit concerns by iterating over data without first building up a large array in memory.
As of Laravel 6, generators are now used when lazy-loading collections.
You may know that a route's handler can return a variety of different data types. For example, to list only a few, we could return a
Response instance, or a
JsonResponse, or a simple string, or an array, or an Eloquent model, or an object with a
__toString() method, or an object that implements the
Responsable interface. Have you ever wondered how Laravel is allowing for this? Let's read the Laravel source code and figure out for ourselves!
Many of your controller actions likely return a call to the
view() helper function. But have you ever researched what it does? As it turns out, the answer is "quite a lot!" Think about it, your Blade views must be compiled to general PHP, cached for future requests, and then converted into the appropriate response. In this episode, we'll once again open the hood and read the Laravel source code.
If you visit your Laracasts profile page, under the "My Feed" section, you'll find a list of the latest Laracasts lessons, grouped according to a relative date. For example, "today," "this week," "last week," and older. In this episode, I'll show you how easy it is to perform groupings like this with Laravel collections and the Carbon library.
If you browse the Laravel codebase, you'll find countless references to a trait called
Macroable. This trait allows you to extend the public interface of a class in real-time. In this episode, we'll review how to use this trait in your own projects, as well as how it works under the hood.
Today's question comes courtesy of Sam, who would like to know how to limit the number of downloads each user in their application can initiate. This question focuses on downloads, however, the technique we use for the solution can of course be applied to any request that you wish to rate limit. While we can solve this manually with basic caching, let's instead leverage Laravel's built in throttling route middleware.
*Series still in development. Check back often for updates.