What’s New In Laravel 5.5

in Laravel

Laravel 5.5 is due to be released in July 2017, in every new release of the frameworks there's always some cool new features to investigate, so let's take a look at what's new.

Laravel Package Auto Discovery

Whenever you include a Laravel package into your application you will always do the same things.

  • Require the package in composer
  • Register the provider in the app.php file
  • Regiter any facade if used in the package

In Laravel 5.5 this is changing to be automatic package developers can now include new settings in the composer.json file which will automatically register the new package.

If you view this pull request by Taylor you'll be able to see what the extra settings are which Laravel will pick up and automatically register the package.

Laravel Debugbar Pull Request

Custom Validation Rules

Laravel 5.5 introduces the use of custom validation rules, which will be located in the namespace of App/Rules.

This class will consist of 2 methods a passes method and a message method. Passes method will define the rules in which passes the validation, the message method is the default message used to show to the user when the validation fails.

To use this new class you can include this in the defined validation rules.

Laravel Fresh Migration

If you need to rebuild your entire database on Laravel you can use the migrate:refresh command which will rollback and rerun all your migration files. With this refresh command Laravel will rollback the database by running the down methods on your migration files.

In Laravel 5.5 there is a new command you can use to rebuild your database this is the migrate:fresh command, which will rebuild your database in a different way. Instead of using the down method on your migration files it will drop all your tables and then rerun the migration files.

This allows you to have a quick and easy way to reset your Laravel development database.

php artisan migrate:fresh

Requires PHP 7.0+

Laravel now requires you to use PHP 7, there are many benefits you get from PHP 7 includes things like speed improves and strictly typed return types, Laravel frameowrk can now take full advantage of the PHP 7 features by requiring this as the minimum PHP version.

If you use Homestead as your development environment then you will already be using PHP 7.

If you don't then you should view this post on php.net for information on how to upgrade.

Migration to PHP 7

Render Mailables To The Browser

When you're developing email templates it can be pain to have to send a new email each time you want to test what it looks like in the browser.

Using mailables you can define your own markdown used in the email.

php artisan make:mail OrderShipped

You can then view this email in the browser by assigning it to a view.

Route::get('/order-shipped-email', function () {
    return new App\Mail\OrderShipped();
});

Frontend Presets

In Laravel 5.3 the framework would include Twitter bootstrap and VueJS for the frontend, this is great if you want to use these two in the application but what if you want to use React or a different CSS framework.

There is now a new artisan command to set the preset for your frontend structure, if you want to remove VueJS and bootstrap you can use the command.

php artisan preset none

Or if you want to use React instead you VueJS you can do so by using the preset react command.

php artisan preset react

Instead of having the default Example.vue file it will be replaced with a React component inside an Example.js file.

Validate Returns Request Data

Using the validate() method in previous versions you will need to something like this.

/**
 * Store a new blog post.
 *
 * @param  Request  $request
 * @return Response
 */
public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'price' => 'required|numeric',
        'category_id' => 'numeric|exists:categories',
    ]);

    // The blog post is valid, store in database...
}

Then after the validate method you need to get the data for only the validated data.

$data = $request->only('name', 'price', 'category_id');

You will almost always want to return the validated data after it's passes validation, so in 5.5 the validate method will return the data that has passed validation.

public function store()
{
    $data = $this->validate(request(), [
        'name' => 'required',
        'price' => 'required|numeric',
        'category_id' => 'numeric|exists:categories',
    ]);

    // $data = request()->only('name', 'price', 'category_id');

    return Product::create($data);
}

Throw Helpers

There have been two new helpers added for throw and exception if a variable is true and throw and exception if the variable is false.

// throw if $foo is true
throw_if($foo, \Exception('Foo is true));

// throw if $foo is false
throw_unless($foo, \Exception('Foo is false));

Subscribe To Newsletter

Get weekly updates to your email