DEV Community

Brian Neville-O'Neill
Brian Neville-O'Neill

Posted on • Originally published at blog.logrocket.com on

Laravel 5.8 release: 10 new features to try

The latest version of Laravel, version 5.8, has recently been released. This version has many exciting new features and it is a continuous improvement of previous versions of Laravel. These features include:

  • Automatic policy resolution
  • Carbon 2.0 support
  • Has-one-through Eloquent relationships
  • Token guard token hashing
  • Cache TTL
  • Scheduler timezone configuration
  • Artisan::call improvements
  • Artisan serve improvements
  • Mock testing helper methods
  • Higher order orWhere Eloquent method

And many more. In this article, I will discuss some of these new features in greater depth.

1. Automatic policy resolution

Policies are one of two primary ways Laravel handles user authorization. They are classes that organize authorization logic around a particular model or resource. In the previous version of Laravel, policies needed to be registered in the AuthServiceProvider as follows:

https://medium.com/media/2fb3dd8e4c7ae2a4c29e2e47f10e4281/href

In this case, the policy we are registering is a policy called TransactionPolicy, which we are entering into the $policies array.

However, starting in Laravel 5.8 you do not need to manually register a model’s authorization policy. Laravel can auto-discover policies as long as the model and policy follow standard Laravel naming conventions and the policy directory is in its default location.

If you have models or policies in locations different from the default Laravel structure, you can register a custom callback using the Gate::guessPolicyNamesUsing method. Typically, this method should be called from the boot method of your application’s AuthServiceProvider like this:

https://medium.com/media/30091a8c5d2328cf93d24167e41e2648/href

2. Carbon 2.0 support

Carbon is a package that extends PHP’s own DateTime class and makes working with dates and time very easy. Laravel 5.8 provides support for the 2.0 release of Carbon. Among the new features of Carbon 2.0 is the CarbonImmutable class and a new Date facade. Let’s see how this works.

Enter the following in the \routes\web.php file of a Laravel 5.8 installation:

https://medium.com/media/9bae81848ab493c02ce2eb0b36a543c3/href

Here we are creating a route carbon which would save the current date in a $date variable and then display it. It then adds three (3) days to the current date and also displays it. If you visit the /carbon route we have just created, you would see something like:

What is happening here is that we are changing our object. This may be what you desire, but in many cases, this is not what we want, as dates are usually protected properties. We should actually be able to create a new date and not modify an existing date. For example say we are storing a date of birth in one of the columns of our database, this is not information that is going to change, however, we may create a copy for it and do some modifications to that copy. This is where the CarbonImmutable class comes in. To use this feature, go to your AppServiceProvider and enter the following:

https://medium.com/media/0bc325a079ab177a98b1185a5308b484/href

Then update the \routes\web.php file to use the new Date facade and create a copy of the date which we can change:

https://medium.com/media/cb99d34aa4540c46a7c42d954aa2ba50/href

Refresh your browser and you should see this:

3. HasOneThrough eloquent relationship

Laravel 5.8 introduces a new eloquent relationship: HasOneThrough. Though this is new in Laravel, it exists in other frameworks like Rails. Say we have three models: a Supplier model an Account model and an AccountHistory model. A supplier has an account and an account has one account history.

Previously to get a supplier’s account history, you will have to find the supplier then write something like: $supplier->account->accountHistory. Now you may use a hasOneThrough relationship to skip this step, accessing a supplier’s account history straight away like this: $history = $supplier->accountHistory through the account model:

https://medium.com/media/972c8ad6e5fb9ac896703e3a96354586/href

4. Token guard token hashing

A little known fact about Laravel API authentication is that you don’t always have to use Laravel Passport. There is a simpler token guard which provides basic API authentication and in Laravel 5.8 it now supports storing tokens as SHA-256 hashes. This provides greater security over storing plain-text tokens.

5. Cache TTL

In previous versions of Laravel, caching was set in minutes. This has changed in version 5.8, to seconds for more precise and granular setting for expiration time when storing items and provide compliance with the PSR-16 caching library standard. So in any reference to cache in your application remember to update to seconds:

https://medium.com/media/06d385e8aa1ff6fa381ba30dd066299a/href

6. Scheduler timezone configuration

In Laravel you can define your timezone for a scheduled task using the timezone method like this:

https://medium.com/media/d849ed8aca1f2095666cf4af4f983251/href

In previous releases, you have to repeat this for every scheduled task and this can quickly become cumbersome if you have many of them. In Laravel 5.8 you can just define a method called scheduledTimezone in your app/Console/kernel.php file and return your default timezone. This will be attached to every scheduler you have:

https://medium.com/media/9b2b0e7c6c4ab91ca315f11497485183/href

7. Artisan call improvements

Laravel allows you to make Artisan commands from your code using the Artisan::call method. In previous versions of Laravel, if you need to pass some options to the command you typically do this:

https://medium.com/media/c3ba29186eff00c6b5f8493015e13b14/href

Now in 5.8, instead of passing the options as an array, you can pass it in one single string like this:

https://medium.com/media/28802dd4f3f3dd857cd161240226bf31/href

8. Artisan serve improvements

A way to quickly serve your Laravel application is by running the command php artisan serve. In previous versions of Laravel this command will run your application in a default port of 8000 and if you attempt to serve another application with the same command, this would fail. Now in version 5.8 the serve command will scan for available ports up to port 8009 so you can serve multiple applications at once.

9. Mock testing helper methods

This is another improvement to make your test code cleaner and readable. Say we want to mock a transaction service and have it return some dummy transaction data. In previous versions of Laravel we would write something like this:

https://medium.com/media/60e5eaba04d08f8df9b42c14320d1f98/href

In Laravel 5.8 this can be shortened to:

https://medium.com/media/5d62913168ebd158be89b31744051ad0/href

This takes care of calling Mockery and binding it into the container. Notice we don’t have to call $this->instance

10. Higher order orWhere eloquent method

Previously if we wanted to combine scoped query with or , we typically would define a closure like this:

https://medium.com/media/33129d7b0ee762b8ad4b8b35214f03e4/href

Laravel 5.8 introduces a “higher order” orWhere method, so you don’t need to write the above closure anymore. Instead, you can write this:

https://medium.com/media/01f97027a48b961f259a0a9ae7c222a7/href

Conclusion

This new version of Laravel comes loaded with many exciting features and we have gone through some of the most notable improvements in the framework. For details on how to upgrade your existing Laravel application to version 5.8, see the upgrade guide. What are your thoughts on Laravel 5.8? Let me know in the comments section!

More resources

If you want to learn more about Laravel’s new features check out the following resources:

Plug: LogRocket, a DVR for web apps

https://logrocket.com/signup/

LogRocket is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.

Try it for free.


Top comments (0)