Laravel 5.7 is live and here's what's new

laravel-jwt-header.png

Laravel 5.7 was released in August. In this article, take a look at some of the exciting new features and improvements, and see how they'll help your Laravel projects.

Introduction

Laravel 5.7 was released almost a month after Laracon US 2018. This version continues with improvements made in Laravel 5.6, introducing Laravel Nova, email verification alongside other cool new features we will be discussing in this article. In this article, we will talk about some cool features of Laravel 5.7.

Laravel Nova

Nova is a beautifully designed admin dashboard panel for Laravel.

As Taylor Otwell Says :

We’ve sweat the small details and carefully crafted Nova to not only look great, but to be a joy to work with.

Laravel Nova

Laravel Nova, which is a Composer installed package can be seen as a standalone service. It makes it possible to build an admin dashboard panel. Is Nova a CMS? Well, it is not. I mean it should be a CMS if all the packages bootstrapped with Nova came out of the box in most content management systems, it would pass for one.

Purchasing a license for this awesome package comes at a whopping $90/site for a solo and $199/site for an enterprise plan. Laravel Nova has a few required packages before installation:

  • Composer
  • Node and Npm
  • Laravel Framework 5.6+
  • Laravel Mix

When the following requirements are met, you may download a Nova release from the release section on the Nova website after purchasing a license.

Email verification

In recent times, there has been a desperate need to validate user emails. In these times, we have painlessly implemented this logic on each and every application that requires email verification. Laravel 5.7 comes with email verification that is configured out of the box to verify email addresses.

This can be implemented in Laravel 5.7 by using the Illuminate\Auth\MustVerifyEmail trait on an App\User model which implements the Illuminate\Contracts\Auth\MustVerifyEmail contract like so:

1<?php
2
3    namespace App;
4
5    use Illuminate\Auth\MustVerifyEmail;
6    use Illuminate\Notifications\Notifiable;
7    use Illuminate\Foundation\Auth\User as Authenticatable;
8    use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract;
9
10    class User extends Authenticatable implements MustVerifyEmailContract
11    {
12        use MustVerifyEmail, Notifiable;
13
14        // ...
15    }

A table that uses email verification must have an email_verified_at column. These columns are already defined in the User table migration file. Adding this column is as painless as running php artisan migrate .

The Auth\VerificationController comes with all the necessary logic needed to verify emails out of the box. To call this controller on routes, all we need to do is to pass the verify parameter to the Auth:routes method like so:

1Auth::routes(['verify' => true]);

Laravel 5.7 comes shipped with a verified method, which can be added to specific routes. To achieve this, a middleware can be configured to only allow a given route(s) to the users. This way, such routes become accessible to users who have verified emails.

1Route::get('profile', function () {
2        // Only verified users may enter...
3    })->middleware('verified');

Testing with Artisan

Laravel now makes it possible to test console applications, which accept inputs. Testing console applications comes off as easy as using three methods, expectQuestion, expectsOutput, and assertExitCode. An example of a simple console application test case is shown below:

1Artisan::command('question', function () {
2        $name = $this->ask('What is your name?');
3
4        $coding_style = $this->choice('Tabs or Spaces?', [
5            'Tabs',
6            'Spaces'
7        ]);
8
9        $this->line('Your name is '.$name.' and you prefer '.$coding_style.'.');
10    });

You may test this console application utilizing the three methods mentioned above like so:

1/**
2     * Test a console command.
3     *
4     * @return void
5     */
6    public function test_console_command()
7    {
8        $this->artisan('code:style')
9             ->expectsQuestion('What is your name?', 'John Doe')
10             ->expectsQuestion('Tabs or Spaces?', 'Tabs')
11             ->expectsOutput('Your name is John Doe and you prefer Tabs.')
12             ->assertExitCode(0);
13    }

Thanks to Michal Putkowski for his contribution, Laravel now has a new method, appended to the paginate method. This method linksOnEachSide eliminates the need for a custom pagination view in most cases.

1<?php
2
3    User::paginate(10)->linksOnEachSide(5);

This means five pagination links will appear on each side of the paginator.

Tweet about pagination

To check out how this was implemented, you can view the PR here.

Symfony dump server

At this years Laracon, Taylor Otwell announced that Symfony dump server will be shipped in Laravel 5.7. Symfony dump server when running redirects all reference to the dump method in a console window. This allows developers to debug and inspect views without contorting HTTP response outputs. To start up the dump server type php artisan dump-server in your Laravel project directory.

Symfony dump server CLI output

Feeling like contributing at the upcoming Hacktoberfest? Symfony dump-server wouldn’t be a bad start.

Callable action URLs

The action method binds a controller to a URL. An action is callable because actions can be referenced in an array syntax. As of Laravel 5.6 action URLs were were defined thus:

1$url = action('FooController@bar');

Callable action URLs were introduced in Laravel, thanks to Sebastian De Deyne. We can reference actions in callable array syntax like so:

1use App\Http\Controllers\FooController;
2
3    $url = action([FooController::class, 'bar'])

PS: Callable action URLs will come in handy when navigating around your code base if of course, your IDE supports it.

Flattened resources directory

Flattened resource file tweet

Yes! Laravel 5.7 comes with a missing parent directory, the assets. In Laravel 5.6, the folder structure was like so:

1resources
2      ├── assets
3      │   ├── js
4      │   └── sass
5      ├── lang
6      │   └── en
7      └── views

In this current version of Laravel (5.7), the resources/assets have been flattened into resources. The folder structure now looks like this:

1resources
2      ├── js
3      ├── lang
4      ├── sass
5      └── views

We no longer need to navigate into the assets directory every time we want to use our js or sass assets. With this new update, we can call our assets directly from the resources folder.

Isn’t that awesome!

Notification localization

Laravel now allows you to send notifications in a new language other than the current or default language, it will also remember this locale if it is queued. This can be implemented by adding the locale method which is now offered in the Illuminate\Notifications\Notification class.

1$user->notify((new emailVerified($user_id))->locale('es'));

This could be used to send a notification to the user in his local language, leveraging on his/her country entered during registration. This locale defaults to its original state after the notification has been sent.

File system read/write streams

Laravel filesystems integration now offers readStream and writeStream methods, which can be implemented like so:

1Storage::disk('rackspace')->writeStream(
2        'remote-file.zip',
3        Storage::disk('local')->readStream('local-file.zip')
4    );

If you are implementing this Illuminate\Contracts\Filesystem\Filesystem contract, you should add the readStream and writeStream methods.

Improved error messages for dynamic calls to Eloquent models

Laravel now sends problem specific error messages.

Improved error messages tweet

Concise error messages will come in handy when debugging Laravel applications. Here’s a link to the pull request submitted by Joseph Silber, if you’re curious on how this was implemented behind the scenes.

Error page redesign

Laravel 5.7 comes with beautifully redesigned error pages for handling HTTP exceptions packed with some sense of humor. Our major focus in this article is the 404 , 403 and 500 error codes.
404

New 404 page

403

New 403 page

500

New 500 page

Conclusion

In this article, we have looked at the new features of Laravel 5.7. You can view the release notes and the upgrade guide, to know how to upgrade your current Laravel application to version 5.7.