Have no fear! Laravel 5.7 is here!

Laravel is actively supported and contributed open source project, that made its first appearance аbout 7 years ago in June 2011. Since then it has made its name among the list of top PHP development platforms for developing web applications following the MVC (Model View Controller) architectural pattern and based on Symfony. It is a top choice of PHP developers and is one of the most popular free open-source PHP frameworks on the market. The source code of Laravel is hosted on GitHub and licensed under the terms of the MIT License.

The long-awaited 5.7 release finally arrived earlier this month (September 4th, 2018) and quickly received tremendous feedback from its large active developers’ community. This release introduces several new features for advanced functionality and many bug fixes and improvements over previous Laravel 5.6 version.

Before we dive in, we’ll quickly note that Laravel framework is said to be rolling out any bug fixes until February 2019 and security fixes until August 2019. Let’s get started!

Laravel Nova

The most awaited Laravel package, officially announced by Taylor during Laracon US 2018, Laravel Nova is now on the market. Released on August 22, 2018, it is undoubtedly the most important and significant package or feature that has been introduced by the development team. To tell you more, it is not a feature of Laravel but rather a separate package that helps to perform various functional operations.

The primary feature of Nova is the ability to administer your underlying database records using Eloquent. Additionally, Nova offers support for filters, lenses, actions, queued actions, metrics, authorization, custom tools, custom cards, custom fields, and more. You can install it in your new project or even in the existing project via Composer to create a beautiful code-driven administrative panel or dashboard for Laravel development. Good news is that Laravel version 5.6 also supports the Nova in the form of a single composer package.

You can now purchase its initial release v1.0.* (Orion) from the official Laravel website. The license for this excellent package comes at a whopping $99/site for a solo and $199/site for an enterprise plan. Note that Laravel Nova has a few required packages before installation:

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

If you want to read more about this package, you can check the detailed article by Taylor Otwell on Medium, or it’s official documentation.

Resources Directory Changes

There is a slight change in the resources directory in Laravel 5.7. The resources directory is now flattened removing the “assets” folder that appeared in previous versions.

Taylor Otwell publicly announced this change in one of his tweets. As assets will no longer be in the Resource directory, instead it will contain JS, Sass, lang, and views.

Email Verification

As you are all familiar with the Laravel default authentication system, Laravel 5.7 introduces a new optional email verification to the authentication scaffolding included with the framework. To utilize this feature, you have to add the email_verified_at timestamp column to the users migration table. You don’t need to add yourself; it comes with default migration. You just need to run the migration.

To suggest newly joined users to verify their email address, the User model should implement the MustVerifyEmail interface. So, your User model would look like below:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
  // ...
}

Once the User model is marked with the MustVerifyEmail interface, all the newly registered users will receive an email containing an account activation verification link. It activates their respective accounts as soon as they click on it. Once this link has been clicked, Laravel will automatically record the verification time in the database and redirect users to a location of your choosing.

Laravel 5.7 also provides a verified Middleware along with this email verification feature. This middleware may be attached to routes that should only allow verified users:

'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,

Guest User Gates / Policies

In previous versions of Laravel, authorization gates and policies automatically returned false for any unauthenticated users trying to access the application. However, with Laravel 5.7 you can now allow declare an “optional” type-hint or supply a null default value to allow the guest user to pass through the authentication checks.

Gate::define('update-post', function (?User $user, Post $post) {
  // ...
});

Notification Localization

Laravel now allows you to send notifications in any other than the current language by assigning locale for it. It will even remember this locale if the notification is queued.

To accomplish this, the Illuminate\Notifications\Notification class now offers a locale method to set the desired language. The application will change into this locale when the notification is being formatted and then revert back to the previous locale when formatting is complete:

$user->notify((new NewUser($user))->locale('np'));

Localization of multiple notifiable entries may also be achieved via the Notification facade:

Notification::locale('np')
->send($subscribers, new WeeklyNewsletter($newsletter));

Console Testing

Laravel 5.7 allows you to easily “mock” user input for your console commands using the expectsQuestion method. Also, you may specify the exit code and text that you expect to be output by the console command utilizing the assertExitCode and expectsOutput methods.

Paginator Links

Laravel 5.7 has a new pagination method to customize the number of links that are displayed on either side of the paginator URL window. Working on pagination before, you would have seen three links in it which are by default. But in Laravel 5.7 you can define them explicitly, giving you more ease to handle pagination precisely. Thanks to the new method you no longer need a custom pagination view in some cases.

Improved Error Messages

Laravel 5.7 makes it much easier to track all error messages in a better way thanks to Bouncer developer Joseph Silber! With the new version, you will get a cleaner and concise text message indicating that the approach doesn’t exist on a particular model. The reason behind this is that the Laravel 5.7 now uses Bouncer package. This will show errors message on dynamic calls to Eloquent models (and other parts of the framework).

URL Generator & Callable Syntax

Laravel 5.7 introduces a new “callable” syntax when generating URLs to controller actions rather than supporting strings only. You might also hear this feature called “tuple notation” or a “callable array syntax” for URL generation actions. One benefit of the new callable array syntax format is the ability to navigate to directly to the controller if you use a text editor or IDE that supports code navigation.

action([UserController::class, 'index']);

Laravel Dump Server

Laravel 5.7 comes packaged with the incredibly useful new PHP artisan dump-server command. Written by Marcel Pociot, this command provides a wrapper around Symfony’s VarDumper component. You get this command out-of-the-box that allows you to dump data as part of your response (examples: dump($users) or dd('hello world')), the output will be swallowed up and printed in the console or an HTML file instead of to the browser.

The Symfony Dump Server is more than useful in debugging an error in the application for which you don’t have to hinder the application runtime. The command runs in the background, and it gathers the data transmitted from the apps and displays output via the console mode.

php artisan dump-server
# Or send the output to an HTML file
php artisan dump-server --format=html > dump.html

Better ways of testing Artisan Command

Laravel 5.7 ships with improved support for testing your Artisan commands. You can now leverage the helpful expectsQuestion() and expectsOutput() methods to ensure that your commands function exactly as expected.

Installing Laravel 5.7

As you can see a lot of cool and exciting stuff is coming along with the new release, so you should be probably eager to try it out by now.

Laravel 5.7 is currently not available in Softaculous for install, but you can still install it manually via Composer. In order to pull the latest version of Composer though, before proceeding with the installation,  so  check whether the needed version of your hosting account is set up to PHP 7.1 +. Overall, PHP 7 is faster, more secure, and significantly more resource efficient than older versions.

Use the following command in your /home/user/.composer directory:

composer global require "laravel/installer"

To create a new instance of Laravel, you will have to go to your public_html and type:

composer create-project --prefer-dist laravel/laravel blog

Where blog is the name of the folder which will be created and in which Laravel will be installed. Now access the folder and execute:

find . -type d -print0 | xargs -0 chmod 0755 && find . -type f -print0 | xargs -0 chmod 0644

This will change the permissions to the desired ones which will allow the /public folder to be displayed correctly and you will be able to open the website on your browser.

Upgrading to Laravel 5.7

Laravel 5.7 requires PHP >= 7.1.3, so before proceeding with the upgrade, make sure the needed version is set up. The estimated upgrade time from Laravel version 5.6 is about ten to fifteen minutes making your mileage vary based on your application.

Summary

Laravel 5.7 came loaded with new features and significant improvements. And Nova is a product every Laravel developer should give a try!

If you enjoyed this article, then you’ll love FastComet’s Laravel SSD Cloud hosting platform. Experience increased power, flexibility, and control for your applications.

Have you upgraded to Laravel v5.7 yet? What are your thoughts? Let us know in the comments section! Happy Coding!

Elena

Elena oversees all Marketing, Product Management and Community efforts for FastComet and is in charge of telling the brand's story. Always pitching, she’ll share the FastComet vision with anyone who’ll listen. Elena helps our customers make the most of their web sites' and focuses on our inbound marketing efforts; everything from developing new online growth strategies, content creation, technical SEO, and outreach within the FastComet community. Her background includes Sales and Customer Relationship development, as well as Online Marketing.