Laravel 5.5 LTS is Now Released


August 30th, 2017


Version 5.5 of Laravel is now officially released! This release is jam-packed with goodies and improvements–here’s a quick video summarizing the highlight features:

Taylor Otwell recenly described his thoughts on Laravel 5.5 in this tweet:

Leave tomorrow for @LaraconEU. 5.5 next week also. Favorite release yet. Love the shape the framework is in. Never been happier with it. ????

— Taylor Otwell (@taylorotwell) August 25, 2017

Laravel 5.5 is the Next LTS Release

Laravel 5.5 is the next long term support (LTS) version of Laravel (the last being 5.1). LTS versions receive bug fixes for two years, and security fixes for three years.

General minor releases receive bug fixes for six months and security fixes for one year.

Whoops Package

You might recall the filp/whoops package from Laravel v4 provided elegant stack traces for your most frustrating debugging moments. The whoops package returns to Laravel 5.5!

Collection Dumping

Another great debugging feature (that I’ve seen previously as a package or macro) is collection dumping methods:

4 ->filter
5 ->platinum
6 ->dump()
7 ->filter(function ($song) {
8 return $song->released_on >= \Carbon\Carbon::parse('-10 years');
9 })
10 ->dd();

Read our collection dumping post for more details.

Exception Rendering

Exceptions now can render a response if they define a public “response” method. Typically, in earlier versions of Laravel you might add a check to the App\Exceptions\Handler::render() method, and conditionally send back a response based on the exception type.

In 5.5, you can just throw the exception, and it can respond without additional logic in your handler:

3// throw new TerribleSongException($song) in a controller...
5namespace App\Exceptions;
7use App\Song;
9class TerribleSongException extends \Exception
11 /**
12 * @var \App\Song
13 */
14 protected $song;
16 public function __construct(Song $song)
17 {
18 $this->song = $song;
19 }
21 /**
22 * @param \Illuminate\Http\Request $request
23 */
24 public function render($request)
25 {
26 return response("The song '{$this->song->title}' by '{$this->song->artist}' is terrible.");
27 }

You can also implement the Responsable interface in your exception classes, and Laravel will respond automatically.

The Responsable Interface

The Responsable interface is another response addition to laravel that we’ve covered at Laravel news. A class implementing the interface can be returned from a controller method; the router now checks for an instance of Responsable when preparing the response from Illuminate\Routing\Router.

Here’s what an example might look like, leaving the response details to the NewSongResponse object:

1public function store(Request $request)
3 $data = request()->validate([
4 'title' => 'required',
5 'artist' => 'required',
6 'description' => 'required',
7 'duration' => 'required|numeric',
8 'released_on' => 'required|date_format:Y-m-d',
9 'gold' => 'boolean',
10 'platinum' => 'boolean',
11 ]);
13 $song = new Song($data);
14 $song->save();
16 return new NewSongResponse($song);

Here’s what the class might look like implementing the Responsable interface for a new song creation:

3namespace App\Http\Responses;
5use App\Song;
6use Illuminate\Contracts\Support\Responsable;
8class NewSongResponse implements Responsable
10 /**
11 * @var \App\Song
12 */
13 protected $song;
15 /**
16 * @param \App\Song $song
17 */
18 public function __construct(Song $song)
19 {
20 $this->song = $song;
21 }
23 public function toResponse($request)
24 {
25 if ($request->wantsJson()) {
26 return response()
27 ->json($this->song)
28 ->header('Location', route('', $this->song))
29 ->setStatusCode(201);
30 }
32 return redirect()
33 ->route('', $this->song);
34 }

In this simple example, you could automatically respond with JSON if you make a request via AJAX, and by default response with a redirect the route.

Request Validation Method

In past versions of Laravel you would pass the request instance to the $this->validate() method in a controller:

1$this->validate(request(), [...]);

Now, you can just call validate on the request object:

1$data = request()->validate([
2 'title' => 'required',
3 'artist' => 'required',
4 'description' => 'required',
5 'duration' => 'required|numeric',
6 'released_on' => 'required|date_format:Y-m-d',
7 'gold' => 'boolean',
8 'platinum' => 'boolean',

Another nice benefit from this style of calling validation is that the return value acts like Request::only(), returning only the keys provided in the validation call. Returning only the validated keys is an excellent convention to use, avoiding Request::all().

Custom Validation Rule Objects and Closures

My favorite feature in Laravel 5.5 is hands-down the new custom validation rule objects and closures. Creating a custom rule object is an excellent alternative to creating custom rules with Validator::extend (which you can still use), because it’s more clear where the rule logic is located at a glance. A validation rule object might look like this:

3namespace App\Rules;
5use Illuminate\Contracts\Validation\Rule;
7class CowbellValidationRule implements Rule
9 public function passes($attribute, $value)
10 {
11 return $value > 10;
12 }
14 public function message()
15 {
16 return ':attribute needs more cowbell!';
17 }

An example of using this validation rule looks like the following:

4 'cowbells' => [new CowbellValidationRule],
5 'more_cowbells' => [function ($attribute, $value, $fail) {
6 if ($value <= 10) {
7 $fail(':attribute needs more cowbell!');
8 }
9 }]

The closure style takes the attribute and value, and a fail parameter that you call if the validation rule should fail. The closure is a nice way to experiment with custom validation before you extract it to a dedicated rule object, or for one-off custom validation needs.

To create custom validation rule objects, you can use the new make:rule command:

1$ php artisan make:rule MyCustomRule

We have a dedicated post to custom validation rules here on Laravel News, be sure to check it out!

Auth and Guest Blade Directives

We have written about Blade::if() directives in 5.5. A few new conditional directives in 5.5 are @auth and @guest.

Typically you might use something like the following to check for an authenticated user in Blade:

2 {{ -- authenticated --}}

You can now use the following directives to achieve the same thing:

2 Welcome {{ user()->name }}!
6 Welcome Guest!

Frontend Presets

When you are starting a new project, Laravel 5.5 provides Vue.js scaffolding by default. In Laravel 5.5 you can now pick from a few presets and remove all frontend scaffolding with the “preset” Artisan command in Laravel 5.5.

If you look at the help, you can see that it allows you to pick “none,” “bootstrap,” “vue”, or “react”:

1php artisan help preset
3 preset <type>
6 type The preset type (none, bootstrap, vue, react)
8# Use react
9$ php artisan preset react
11# Clear scaffolding
12$ php artisan preset none

Separate Factory Files

Factory files were previously defined in one ModelFactory.php file. Now, you create different files for each model. You can create a factory file when you are creating a new model:

1$ php artisan make:model -fm Post
3# Or you can create a controller, migration, and factory
4$ php artisan make:model --all

You can also create a factory file directly with “make:factory”:

1$ php artisan make:factory --model=Example ExampleFactory

The migrate:fresh Migration Command

The new “migrate:fresh” migration command 5.5 is a nice addition to creating a clean database in development. The migrate:fresh command drops all the database tables and then runs the migrations.

You might be familiar with the existing migrate:refresh command, which rolls back migrations and then reruns them. Usually in development you want to just drop the tables, getting a fresh database, and running migrations.

The RefreshDatabase Trait

On the testing front, the RefreshDatabase trait is the new way to migrate databases during tests. This new trait takes the most optimal approach to migrating your test database depending on if you are using an in-memory database or a traditional database. The DatabaseTransactions and DatabaseMigrations traits are still available in 5.5, allowing you to upgrade without using the new RefreshDatabase trait.

The withoutExceptionHandling() method

The base test case inherits a method withoutExceptionHandling(), which allows you to disable exception handling for a test. Disabling exception handling allows you to catch the exception in your test and assert the exception instead of the exception handler responding. It’s also a useful debugging tool when your test is doing something you don’t expect, and you want to see the actual exception.

Automatic Package Discovery

The last feature we are going to look at is automatic package discovery. While Laravel packages aren’t usually hard to install, the package detection feature means you don’t have to set up providers or aliases. You can disable auto-discovery for specific packages.

Learn more about this feature from the Taylor Otwell’s article on this feature, and on our post.

Learning More About Laravel 5.5

Make sure to check out our Laravel 5.5 content for a more in-depth look. Laracasts has a complete series available on all of these features. Also, check out the official documentation, release notes, and the upgrade guide.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.