What's new in Laravel 5.3
Laravel, which is probably the single most popular PHP framework in use today, has just release a new version. Coming about 10 months after the previous 5.2, Laravel 5.3 has some pretty exciting new features. So let's have a look at some of them now.
First up—new home page. OK, I admit that's not really so exciting. But it does give us a little peak at the new features that Laravel chose to display, namely these three:
Laravel Scout:
Designed to be used with Eloquent models, Laravel Scout allow you to add full text search to them via a straightforward, driver based solution. Scout comes complete with an Algolia driver, but is extensible allowing users to write custom drivers and make their own search implementations.
Scout will implement a “Searchable” trait with your existing models. After that we simply sync up the data with the search service as shown here:
php artisan scout:import App\\Post
You can search models with:
Post::search('Pedro')->get();
You can use pagination like so:
Post::search('Pedro')->paginate()
And even throw in a basic where clause:
Post::search('Pedro')—>where('acount_id', '>', 1)->paginate()
Cool. For all the details of Laravel Scout check out this link.
Next up is Laravel Echo which is a JavaScript library intended to improve upon the current event broadcasting system. Install Echo with the NPM packet manager and integrate with pusher-js:
npm install --save laravel-echo pusher-js
Once you've got it installed, you can create a new Echo instance in your apps JavaScript. It's recommended to do this is at the bottom of the resources/assets/js/bootstrap.js file that is included with the Laravel framework:
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your-pusher-key'
});
For more, see this video by the man himself, Taylor Otwell.
Number three: Laravel Passport aka API Authentication. This can be a difficult area with OAuth2 being the common standard. And even with the help of good packages, it can still be pretty tricky. If you’re not familiar with OAuth2, you should get caught up before using this feature.
Passport is installed by way of the Composer package manager, like so:
composer require laravel/passport
API Auth is a pretty involved subject and beyond the scope of this article. But if you're into it, goto Laravel's site for full installation instructions and more info: laravel.com/docs/5.3/passport#installation
So those are the three feature on the new homepage, but what new other surprises does Laravel have instore for us?
Migrations
One small but significant new feature was added to Laravel's migration which allows for the rollback of a single step as opposed to the entire migration. A new step flag was added to artisan allowing for the following:
php artisan migrate:rollback --step=2
There is also now the options to have multiple migration paths instead of using a single migration folder. To add another migration directory:
$this->loadMigrationsFrom('path/to/migrations/folder');
The Blade Loop Variable
A new loop variable was added to Laravel Blade allowing you to fine tune your loops a bit. It has the following properties:
- index – The number of the current item in the loop
- remaining – How many items remain in the loop
- count – number of total items in the loop
- first – Boolean returns true if this is the first item in the loop
- last – Boolean returns true if this is the last item in the loop
- depth – How many levels deep the loop is
- parent – Allows you to call the parent loop item in a nested loop
So you can now code something like this:
@foreach ($states as $state)
@if ($loop->count)
@if ($loop->first)
<li class="list-item list-item--header">{{ $state->name }}</li>
@elseif ($loop->last)
<li class="list-item list-item--footer">{{ $state->name }}</li>
@else
<li class="list-item">{{ $state->name }}</li>
@endif
@endif
@endforeach
OK, now that we're all <wait for it> in the loop, <sorry> let's move on.
Query Builder
In previous versions, the query builder only returned an array. However, in 5.3 it will return a collection of objects. This is nice so that we don't have to loop through the array. Note though that since the default has been changed to return a collection, this is potentially a breaking change.
Mailable Objects are now supported in 5.3. This is a class based approach for sending e-mails that enables you to represent messages as simple objects. Once a mailable object is defined, it allows for something like this:
Mail::to('abcMail@gmail.com')->send(new PaymentReceived);
There are several other new features in Laravel 5.3, but I'll cut it here before I'm tempted to make more bad puns. One last thing worth mentioning.
If you're interested in upgrading from 5.2 to 5.3, better clear the rest of your afternoon; Laravel's site estimates 2-3 hours. Also, this:
"We attempt to document every possible breaking change. Since some of these breaking changes are in obscure parts of the framework only a portion of these changes may actually affect your application."
You've been warned—now get to it!
Recent Stories
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}
{{CommentsModel.TotalCount}} Comments
Your Comment