What's New in Laravel 5.5

Laravel 5.5 will require PHP 7.0+.

Sreamlined Request Validation

  • you can now directly call the validate method on your Request instance.
  • You also no longer need to pass the request as the first argument to the validator.
  • The second change made is that the validator returns the request data which you can store in a variable and pass on to the create method of the model.
public function store()
{
    $post = request()->validate([
        'title' => 'required',
        'body' => 'required'
    ]);

    // $data = request()->only('title', 'body');

    return Post::create($post);
}

You need to be careful with this since the data returned by the validator will only contain the fields defined in the rules. This adds a little security but you can loose data if some fields were not defined rules for. To avoid this trap, you can add the field with an empty rule like this:

public function store()
{
    $post = request()->validate([
        'title' => 'required',
        'body' => 'required',
        'fieldWithNoRules' => '',
        'andAnotherOne' => ''
    ]);

    // $data = request()->only('title', 'body');

    return Post::create($post);
}

Email Themes

Laravel 5.5 adds the ability to specify a custom theme directly to Mailable classes. You can create a CSS style-sheet like this:

touch resources/views/vendor/mail/html/themes/custom.css
// And then specify this filename as a property in your Mailable class.
class Welcome extends Mailable
{
    protected $theme = 'custom';
    ...
}
// app/Mail/Welcome.php

the email layout will be based on the styles we defined in the custom.css file. The good thing with this approach is that we can have different themes for different mailables.

Fresh Migrations

Laravel 5.5 adds another Artisan command to the migrate: namespace. This is similar to migrate:refresh but rather than rolling back your existing migrations, it drops all tables and migrates them from start.

php artisan migrate:fresh

Automatic Package Discovery

In Laravel 5.4 and all versions before, pulling a Laravel package required registering its service providers and adding aliases. Laravel 5.5 adds the ability for packages to automatically register service providers and add aliases through the package's composer.json file like this:

"extra": {
    "laravel": {
        "providers": [
            "Foo\\Bar\\ServiceProvider"
        ],
        "aliases": {
            "Bar": "Foo\\Bar\\Facade"
        }
    }
}

Model Factory Generators

  • In Laravel 5.5 you can easily generate model factories with a new Artisan command called make:factory.
php artisan make:factory PostFactory
  • you can also generate a factory while making a model.
php artisan make:model Post -f

Custom Validation Rules

  • To define a custom validation rule, you need to create a class with two methods: passes and message.
  • The passes method accepts two arguments: attribute and value, which you can use to validate the field.
php artisan make:rule CustomRule

class CustomRule implements Rule
{   
    public function passes($attribute, $value)
    {
        // must return true or false for the validation to pass or fail
          return $value !== 'unwantedname';
    }
    public function message()
    {
        // return a string here for the failing condition
    }
$request->validate([
    'someField' => [
        'required', 'min:4', new CustomRule()
    ]
]);

DD and Dump Coming to Collections

  • Laravel 5.5 as we can now call dd() or dump() directly on a collection making debugging a lot easier.
$posts = Post::all();

 $posts
    ->dump()
    ->pluck('title')
    ->dump();
// output 
Collection {#284 ▼
    #items: array:3 [▼
        0 => Post {#285 }
        1 => Post {#286 }
        2 => Post {#287 }
    ]
}

Collection {#272 ▼
    #items: array:3 [▼
        0 => "Aida Bosco"
        1 => "Madge Leuschke"
        2 => "Miss Bulah Armstrong Jr."
    ]
}

there’s a difference between calling dump() and dd() on a collection. dump() outputs the results at that moment and then continues processing while dd() stops the process immediately and dumps out the results (dd stands for dump and die).

New Routing Methods

Route::view('/welcome', 'welcome');
Route::redirect('home', 'dashboard');

The first route will map the welcome view to the /welcome path while the second will redirect any requests made to /home to the /dashboard.

Exception Helper Functions

Coming to Laravel 5.5 are two new helper functions to help you throw exceptions more elegantly. The two helpers are : throw_if and throw_unless methods. Both take three arguments with the third argument being optional.

// For `throw_if:
$number = 2;
throw_if($number !== 3, new NotThreeException('Number is not three'));
// or
throw_if($number !== 3, NotThreeException::class, 'Number is not three');

// For `throw_unless:
$phoo = false;
throw_unless($phoo, new BazException('Phoo is false'));
// or
throw_unless($phoo, BazException::class, 'Phoo is false');

Frontend Presets

  • Laravel 5.5 introduces 3 frontend presets: Bootstrap, Vue, React and an option choose your own. With Vue being the default preset, you can change the preset to react using this command:
php artisan preset react

Error Pages: Design Improvement

  • In Laravel 5.5, a little change is made in the design of the error pages like 404 or 50* errors: there are some design additions with Flexbox getting the error message centered on the page.
laravel series1 - What's New in Laravel 5.52 - What's New in Laravel 5.6
LARAVEL#LARAVELPHP#PHP

Comments (0)