Let's start off with some of the smaller changes, and then work our way up to the big additions! To begin you'll notice that your main directory structure is slightly different! A new routes folder at the top level? And what happened to the events, listeners, jobs, and policies folders? What's going on!?
We're still taking it slow. What kind of series would it be if we discussed the coolest additions first? Anyways...next up, we have a small, but important change to be aware of. As of 5.3, all query builder calls will return collection objects, rather than arrays. If you think about it, this will make the general Laravel API more consistent, as relevant Eloquent calls already return collection objects.
In this episode, I'll introduce you to a funky little new
cache global function (gasp!) in 5.3, written by yours truly.
resources/assets/js folder to check it out. It's important to note, however, that this is all entirely optional. If you prefer Ember or Backbone, delete the contents of your main
app.js entry point, and you're good to go! It's only a suggestion.
Out of the box in Laravel 5.3, pagination will still feel very similar to you. However, should you need to modify the underlying HTML, the process is now significantly easier. In fact, you'll find that the process is very similar to how it was in Laravel 3!
Laravel 5.3 "mailables" are thin wrappers around the traditional syntax for sending mail. I think you'll find that they're easier to work with, and much easier on the eyes. Let me show you the essential in this episode.
This next addition to Laravel 5.3 is a small, but useful one. When using the
forelse Blade directives, you'll now have a new
$loop object available to each iteration. This will provide useful information about the loop, including the current index, if it's the first or last item, if there are remaining iterations for the loop, and more.
In Laravel 5.3, you'll find a new
toggle() method on all
belongsToMany relationships. This solves the issue of needing to delete a pivot table record if it exists, or adding it if it doesn't. As an example, consider needing to toggle a user's "like" status for a post.
Notifications in Laravel 5.3 are incredible. Create a single a notification class, and then designate one or more distribution channels to use - such as email, SMS, Slack, and more. I think you'll really like this new component, so let's take a few episodes to review all of its functionality. We'll start with basic email notifications, and review the nice, fluent API for rapidly building up messages.
Let's move on and review the database channel for sending notifications. With this approach, any notification you send will be stored in the database, at which point you can fetch them and display them for the user. In this episode, we'll review the entire process - while also leveraging polymorphism to render each notification type within our views as cleanly as possible.
Let's learn how to use Laravel 5.3's Slack notification channel to notify us, the administrator, each time a payment is processed in our application. As you'll quickly see, it's all a cinch!
Admit it: you always have to do a bit of research to remind yourself how to upload files, determine the temporary folder, guess the file extension, etc. Well now, I promise: you won't forget. In Laravel 5.3, the
UploadedFile instance offers a new
store() method for rapidly saving files - even all the way to S3 (as we'll do in this video).
Closure-based commands are a useful alternative to generating full Artisan command classes. You'll find that this approach is quite similar to how you do your routing (closures vs. controller classes). Want to pass a simple closure to handle a command? Well now you can!
Laravel Scout is a driver-based solution for performing advanced, full text search on your Eloquent models. Out of the box, it comes with support for Algolia, which offers a fantastic service. In this episode, we'll learn how to set everything up, and send a table full of records to Algolia. For the more curious among you, we'll also, toward the end of the video, dig into the guts of Scout to figure out how everything is put together behind the scenes.
Now that you understand how to setup and import your Eloquent records, let's figure out how to query them. You'll find that Scout attaches a global query scope to your models, which adds a useful
searchable() method. While we're reviewing this, we'll also take some time to review custom rankings with Algolia.
In Laravel 5.3, a number of facades provide a static
fake method that can be called to replace the underlying class with a minimal fake version, for the purposes of testing. A fake is a type of test double, where you create a light, barebones implementation of a particular API or interface in order to satisfy or assist a given test.
Consider a scenario where the code you're testing fires an event. Well, it's possible that you don't want the associated event listeners to be triggered, as part of that test. Instead, we can use a double: replace the underlying event dispatcher with a fake version. Now, we can assert that the necessary event was triggered, without worrying about any of the side effects from firing an actual real event. It sounds complicated, but I assure you that it's all very simple.
Imagine that you need to define a validation rule which specifies that the provided email address must be unique on the users table, limited to the email column, where the person's active status is set to
true, but excluding the current user's email address. As you may know, traditionally, we must provide a comma separated list within a string for all of these parameters. It's pretty rough! But not anymore. Now, in Laravel 5.3, you can use the useful
Rule class to fluently build up these constraints.