Clevon
July 25, 2017 • code

4 Laravel 5.5 Features I am looking forward to using

4 Laravel 5.5 Features I am looking forward to using

Laravel is one of the most well-documented, innovative, easy-to-use PHP frameworks I’ve ever used. The framework allows the ease of creating a scalable, stable and robust application coupled with the excellent community support. As Laravel 5.5 about to be released here are 4 features, in no particular order, I am looking forward to using.

1. Custom Validation Rules

As an alternative to using Validator::extend for creating custom validation, Laravel 5.5 will be introducing support for custom validation rule object.

To define a custom validation rule, you can use a class which implements the Illuminate\Contracts\Validation\Rule interface or use a Closure.

<?php
use Illuminate\Contracts\Validation\Rule;
class GrenadaValidationRule implements Rule
{
    public function passes($attribute, $value)
    {
        return $value === 'Grenada';
    }
    public function message()
    {
        return ':attribute must be Grenada';
    }
}

Using the rule like so

<?php
use App\Rules\GrenadaValidationRule;
public function store() {
    request()->validate([
        'country' => [
            'required',
            new GrenadaValidationRule(request('country'))
        ]
    ]);
}
// or
public function store() {
    request()->validate([
        'country' => [
            'required',
            function($attribute, $value, $fail) {
                if ($value !== 'Grenada') {
                    $fail(':attribute must be Grenada!');
                }
            }
        ]
    ]);
}

2. DD and Dump in Collections

When I am debugging a complex collection pipeline, it’s a little challenging. Frequently, I find myself temporarily commenting out pieces of code to confirm the output. In Laravel 5.5, however, you’ll see two new helper methods on the collection instances: dump() and dd(), which will allow you to know what happens in each step of the chain by either using dump() or dd() method.

collect([1,2,3])
  ->map(function($number) {
    return $number * 2;
  })->dd()->reject(function($number) {
    return $number < 3;
  });

Dump

collect([1,2,3])
  ->dump('original')
  ->map(function(int $number) {
    return $number * 2;
  })
  ->dump('modified')
  ->dd();

3. Faster Email Layout Testing

When I prepare the view for my emails, generally uses a service like Mailtrap to investigate the output. In Laravel 5.5, I can return a mailable instance directly from the route and review the output.

$router->get('mail/preview', function() {
   return new OrderConfirmation($order);
});

Result

Laravel Mail Preview

4. Fresh Migration

Migrate fresh is similar to migrate:refresh, however, migrate:fresh drops all the tables and migrate the database to start from scratch. This will help me when I need to rebuild your database during development.

Laravel Migrate Fresh

Proudly hosted by the GREX data center