What’s New in Laravel 5.8

What's New in Laravel 5.8

Laravel 5 was released on February 2015 and current Laravel 5.7 was released on September 2018. We should be expecting to see the initial release of Laravel 5.8 sometime in March 2019.

In this post, I will take a quick look at some of the new features or important changes that you should be aware of before upgrading to the latest release.

Before upgrading Laravel to latest version, always be sure to carefully read the upgrade guides to ensure a smooth upgrade process.

So here are some of the latest changes coming in Laravel 5.8.

Artisan Serve Improvements

Currently when we run php artisan serve command, Laravel server your application on port 8000. If another serve command is running and listening on this port, an attempt to serve the application will fail. In Laravel 5.8, php artisan serve command now scan for available port up to port 8009, which will allow you to serve multiple applications.

Email Validation

Email validation in Laravel 5.8 will allow using international characters in email addresses. If you have set the email field validation to be an email address then, in Laravel 5.7 email validation for [email protected]är.se will fail. However, in Laravel 5.8 it will pass.

Eloquent HasOneThrough Relationship

In Laravel 5.8, we will have a new relationship called HasOneThrough. For example, if we have a Comment model which has one User and an User model has one Profile model. We can use HasOneThrough relationship to get the profile for a given comment. Like below,

/**
 * Get the user profile for the comment.
 */
public function profile()
{
    return $this->hasOneThrough(Profile::class, User::class);
}

Dotenv 3.0 Upgrade

Laravel 5.8 will support, Dotenv 3.0 to manage your project’s environment file. The key feature of the new version is support for multiline strings and white space at the end of strings in your environment file.

Auto Discovery of Model Policies

When we create a new Policy in Laravel 5.7, we have to register it in AuthServiceProvider’s $policies array. Laravel 5.8 introduces auto-discovery of model policies as long as the model and policy follow standard Laravel naming conventions.

Default Scheduler Timezone

When we define a scheduled task we can use the timezone method like below,

$schedule->command('inspire')
         ->hourly()
         ->timezone('Europe/London');

What if you have a number of tasks and you have to set the timezone on every one of them?

In Laravel 5.8, you can define a new method called scheduleTimezone in app/Console/Kernel.php file. This method should return the default timezone which should be assigned to all tasks.

/**
 * Get the timezone that should be used by default for scheduled events.
 *
 * @return \DateTimeZone|string|null
 */
protected function scheduleTimezone()
{
    return 'Europe/London';
}

Higher Order orWhere Eloquent Method

In previous versions of Laravel, we can combine the model scopes using or query operator which requires a Closure callback. Like below:

$posts = App\Post::popular()->orWhere(function (Builder $query) {
        $query->active();
})->get();

Laravel 5.8 introduces a “higher order” orWhere method that allows you to fluently chain these scopes together without the use of Closures:

$posts = App\Post::popular()->orWhere->active()->get();

Blade File Mapping

When you are debugging errors in your application, going through the compiled views is always a pain. In Laravel 5.8, now adds a comment on the top of the compiled file which contains the path to original blade view.

Carbon 2.0 Support

In Laravel 5.8, you will have the option to use Carbon 1 or Carbon 2 for date and time functions. Check the Carbon Documentation for more details.

Caching — TTL

If you are using Laravel’s caching functions take note that if you are passing an integer to the cache function in 5.8 it will apply the time to live in seconds and not minutes as it currently is in 5.7, so this command:

Cache::put('foo', 'bar', 30);

Will store the item for 30 minutes in Laravel 5.7 and 30 seconds in Laravel 5.8. A simple but important difference!

So that pretty much cover the main changes coming in Laravel 5.8 which you should be aware of. You can check for more details about all new features and changes at the Laravel Documentation‘s release notes section.

Leave a Reply

Your email address will not be published. Required fields are marked *

*When sharing a code snippet please wrap you code with pre tag and add a class code-block to it like below.
<pre class="code-block">you code here</pre>

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Laravel Mail Viewer Package
Laravel Mail Viewer is a package by Harish Toshniwal which enables you to view all the mailable in your Laravel...