Debunking Laravel Misconception0:00
(upbeat music) Let's address the final misconception. Laravel is a training wheels framework, and that it's fine for learning, fine for simple CRUD apps, but serious applications require something more sophisticated. This one's interesting because it contains a genuine observation that gets misinterpreted. Laravel is a welcoming framework for beginners.
Speeding Up with Octane0:22
that gets misinterpreted. Laravel is a welcoming framework for beginners. The documentation is approachable, the defaults are forgiving, but somehow that gets read as that it's not capable for professional work. And that's simply not true at all. So we're gonna look at some things that are not for beginners. The first is Laravel Octane,
that are not for beginners. The first is Laravel Octane, and I would love to show this to you, but I'm on Windows and it doesn't run very well on Windows. So Octane, as its name implies, is to speed up your application because most PHP applications run using a traditional model. There's a bootstrap for every request, and then there's a tear down after the response.
There's a bootstrap for every request, and then there's a tear down after the response. So there's that overhead for every request, and that's fine for many workloads, but it's incredibly inefficient for high throughput applications. So Octane keeps your application running in memory. So there's one bootstrap, and then thousands of requests are handled by persistent workers.
and then thousands of requests are handled by persistent workers. You can install it. Well, you can require it, and then you can install it. And the beautiful thing about Octane is that there is literally no change to your code. It is the same exact application that you would run anywhere else, except that if you're running it using Laravel Octane, it's gonna be fast.
Managing Queues with Horizon1:50
except that if you're running it using Laravel Octane, it's gonna be fast. And again, it's not something that every application needs, but if you have a high demand application, Octane is something that you should consider. Next, let's talk about Laravel Horizon, which again, I would love to show you, but it requires Redis, and it's used for monitoring and managing, well, your queues in your application. In fact, if we scroll on down,
well, your queues in your application. In fact, if we scroll on down, we can see this dashboard here so that you can watch the jobs that are running. You can manage them, look at your metrics, see what jobs have failed. You can actually retry them as well. It's a fantastic tool. And once again, you just require it for your application, you run it, or rather you install it,
Feature Flags with Pennant2:34
And once again, you just require it for your application, you run it, or rather you install it, and then you can access the dashboard to manage and view your queues. Another Laravel package that you might want to use is called Pinnit. So we are going to require Laravel Pinnit. And this is a tool that provides sophisticated feature flag management. So as you roll out new features,
sophisticated feature flag management. So as you roll out new features, you can move them into production at whatever pace that you want. You can assign certain users to have access to certain features and all of that great stuff. Except it helps if you type Composer correctly. So Laravel Pinnit. Then we need to call Artisan to vendor publish the Pinnit provider, which is Laravel Pinnit
Then we need to call Artisan to vendor publish the Pinnit provider, which is Laravel Pinnit and then Pinnit Service Provider. And then with that, we need to perform a migration because this is going to add a features table to our database. Then from there, we can just go to our app service provider. And inside of the boot method, we can define a feature. So we would use the feature facade. This comes from Pinnit.
So we would use the feature facade. This comes from Pinnit. And we would define whatever feature that we wanted. So let's say that we have a new version of the API. And we want to roll it out to a set of users. And we can control what users are going to get to that new feature or we could just be completely random and use a lottery. So we could call lottery odds one to four. And we of course need several use statements here.
So we could call lottery odds one to four. And we of course need several use statements here. We need our user model. We need the feature and we also need the lottery. But that's basically it so that we can define a feature. We can assign that feature to users. And then inside of a controller or a route, we can check if the user has that given feature assigned to them, then we can do something. So let's do feature and we will.
assigned to them, then we can do something. So let's do feature and we will. And we're not going to actually run this because I don't have any users. So this is just for theory purposes. So we would check if feature is active and then we specify the feature that we want to check V2, then we could do something. We could, well, it doesn't make sense to return a view with an API.
We could, well, it doesn't make sense to return a view with an API. But we'll return a view with the API. Otherwise, we would return something else like view one. And then we would have view two. Again, it doesn't make sense, but that's the general idea behind the pennant. And checking for these features can be done inside of controllers and some services. It can even be done inside of views.
inside of controllers and some services. It can even be done inside of views. So we have complete and total control over how we get those features to the users who have been assigned those features. So it's a fantastic little package, especially if you want to do A/B testing or if you just want to roll out new features to users at a slower pace, then just giving it to everyone all at once.
Real-Time Events with Reverb5:55
to users at a slower pace, then just giving it to everyone all at once. Another package that you might want to consider is reverb. Reverb allows you to broadcast real-time events. And real-time isn't beginner territory. When your app needs live updates, like status changes or notifications or dashboards, things like that, then reverb is what you can use. Like with every other package, composer,
then reverb is what you can use. Like with every other package, composer, require, larval, reverb. And this is going to ask us a few questions as it starts to pull down that information. Or maybe it won't. Maybe it will whenever we install reverb. I think it's when we install it. We're gonna see regardless of what happens here. First, we're asked which driver that we want to use.
We're gonna see regardless of what happens here. First, we're asked which driver that we want to use. I want to use the reverb driver. Would you like to install and build the node dependencies required for broadcasting? Yes. Would you like to enable the reverb broadcasting driver? Yes. And then from there, you know, we could start listening for those events,
And then from there, you know, we could start listening for those events, but let's create any events. So we will make event. Let's just call this order shit. And then once we create that, then we will fire up the reverb server. We'll use artisan and call reverb start. So this is going to start the server. While that's doing that,
So this is going to start the server. While that's doing that, let's open up our order shit event. And let's say that we want an order ID, because if we are going to send out an event for shipping an order, having that order ID would be useful. So it's just going to be an ints and we'll call it order ID. Now we can change the channel that we want to broadcast on.
and we'll call it order ID. Now we can change the channel that we want to broadcast on. I'm just going to leave the defaults here. Although we can change the channel name, let's do something like orders or something like that. Then from there, it's just a matter of firing off that event. So let's define a routes. The URL will be just slash ship. And then we want to fire off that event.
The URL will be just slash ship. And then we want to fire off that event. So we will new up order shipped. We don't really have an order ID. So I'm just going to pass an integer because that's really all that we need there. And then from there, we'll just say that it was broadcasted and true. And that's it. So if we go to the browser, we go to ship,
And that's it. So if we go to the browser, we go to ship, then we are, of course, going to fire off that event. But unfortunately, we won't see any indication that anything happened. And we, of course, don't have anything listening for that event on the client, but there it is. And it's there and available for us to use. Next, let's look at how we can use eloquent casts. Because, you know, we can store data as primitive values.
Custom Eloquent Casts8:42
Next, let's look at how we can use eloquent casts. Because, you know, we can store data as primitive values. And most of the time, that's what we do. But when it comes to working with those values inside of our code, well, a lot of times we want something a little bit more sophisticated. So we're going to make a model called invoice. And we're also going to create a migration. And let's first of all open up
And we're also going to create a migration. And let's first of all open up that create invoices file. So that all we are going to have is an integer value called total sense. Because that makes total sense. So if you're thinking we're going to use this with our money class, then you'd be correct. So with that done, let's go ahead and let's migrate our database
So with that done, let's go ahead and let's migrate our database so that we have that new table, but something went wrong. Of course, int doesn't exist. It should be integer, different language. All right, so with that, we should be able to migrate our database. There it is. So now we want to essentially cast
There it is. So now we want to essentially cast this total sense value as a money value inside of our application. And we can do that, first of all, by opening up our invoice model. Now, first we need to set up the protected fillable array so that we can mass assign the total sense field. 'Cause otherwise we'd have some issues creating that. And then we're gonna have another protected casts array.
'Cause otherwise we'd have some issues creating that. And then we're gonna have another protected casts array. And this is where we will say that we want to take the total sense value from the database and we want to cast it to something like our money class. We can't do that just yet. And what we are going to do is create a class that's going to make that rather easy to do. So we'll use artisan to make a cast. We'll just call this money cast.
So we'll use artisan to make a cast. We'll just call this money cast. So with that done, we can open up the money cast file. And here we want to change our get method. This is where we would take the value coming from the database and then we would return our money. So let's go ahead and let's add that class reference there so that we will return new money. We need to cast this as an int
so that we will return new money. We need to cast this as an int and that's going to essentially cast the total sense value from the database into a money object. We'll see how that works here in a moment. Now, when it comes to essentially doing the reverse, we want to take the money object that we have and we want to cast that back into an integer so that we can save it in the database. So here, let's change the return value of this method to int
so that we can save it in the database. So here, let's change the return value of this method to int because that is what we are going to return. And then we're going to check if the value is an instance of money, then we want to return the value sense property because that's where our data is. Otherwise, if it's not a money instance, we want to try to cast that as an int so that we can store it inside of the database.
we want to try to cast that as an int so that we can store it inside of the database. So that's our cast. Data coming from the database, we cast into a money object. Data going into the database, we are taking that value if it's money, we return the sense if it's not, then we just cast it as an int. So here back inside of our model,
then we just cast it as an int. So here back inside of our model, we can say that the money sense is going to use our money cast to cast that value. Okay, so we will add that use statement there. So now we can just see this in action by going to our routes and we'll just define another route and we'll have just slash cast as the URL. We don't really need to do anything fancy here. What we will do, however, is create a new invoice
We don't really need to do anything fancy here. What we will do, however, is create a new invoice and we will use our invoice model to create that invoice with the total sense set to a new money instance. Remember, because if we are going to store data into the database, well, we could use an integer value here, but since we can cast from money, let's do money and let's do 2,500 cents. Once again, that's $25. So this will create an invoice with the sense
Once again, that's $25. So this will create an invoice with the sense as being 2,500 and then from there, we can do this. We will see the raw value and to get the raw value, we will call the get raw original method. We will say that we want the raw value of the total sense and that gives us the raw value. But then, you know, since we have a money object now, we can see the formatted version of that. So we can say total sense and then we could call,
we can see the formatted version of that. So we can say total sense and then we could call, I don't remember what that was, it just formatted? Yes, it was formatted. So we can call the formatted method and that will show the formatted currency. So we can hop to the browser, we can go to slash cast. We can see that the raw value is 2,500, but formatted, it is USD $25. So yes, Laravel is beginner-friendly,
it is USD $25. So yes, Laravel is beginner-friendly, but beginner-friendly doesn't mean beginner-only, octane, horizon, penance, broadcasting, custom casts. These are real solutions to real complexity. Laravel's limitation isn't that it can't scale up. It's that a lot of developers never realize how far it already goes because the on-ramp is, well, it's so smooth.
well, it's so smooth.
