Get a Free Quote
Our Blog

Checkout Laravel’s 5.8 Exciting New Features

397 total views
Pankaj Sakariya
April 12, 2019
Checkout-Laravel’s-5.8-Exciting-New-Features

Laravel, an out-standing PHP framework created by Taylor Otwell back in 2011 is the best amongst all PHP framework. It is accessible, powerful, robust and weaves some amazing applications.

This PHP framework facilitates developers to implement MVC pattern abstracting the complex functionalities. Updates are imperative and are only for the betterment of the application life cycle.

Laravel roughly releases an update every 5-6 months and this year too, Laravel released the 5.8 version (March’19) and is not available for the use. You can hire a Laravel developer to get the task done at the earliest.

The continuous improvement from the previous versions has brought some of the exciting features for the developers. Let’s have a sneak peek at some of the amazing features which this new version will offe

  • Automated policy resolution
  • Carbon 2.0 support
  • Has- one-through Eloquent relationships
  • Token guarding and hashing
  • Cache TTL
  • Scheduler Timezone configuration
  • Artisan:: call improvements
  • Mock/spy Testing helper methods
  • Artisan serve improvements
  • Higher order eloquent method
  • dotenv 3.0
  • New error Handling templates

Automated policy resolution:

Earlier Laravel versions had models or policies in non-conventional locations and had to be registered in the AuthServiceProvider class. In this version, the models and the policies will be stored in conventional locations.

You no more have to manually register the model’s authorization policy. This new version will automatically discover policies by following standard Laravel naming conventions.

If the policies you want to register are located in some other location, register a call back by using

Gate::guessPolicyNamesUsing.

You can call this method from the booth method of AuthServiceProvider in the following way:

use Illuminate\Support\Facades\Gate;
Gate::guessPolicyNamesUsing(function ($modelClass) {
// return policy class name...
});

Carbon 2.0 Support:

Good news for the developers, now you can make your application use CarbonImmutable as default.

This package stretches PHP’s Date and Time class and makes the working with Date and Time very easy. The selection of the class depends on the user.

HasOneThrough Eloquent relationship:

This new feature is almost similar to the one offered by Rails. Let’s take an example, Suppose we have three models, Supplier, Account and Account History model. In simple words, a supplier has an account and an account has one account history.

In earlier versions, to get the account history we used to write: $supplier->account->accountHistory. But now through this eloquent relationship, you will straight away write

$history = $supplier->accountHistory

in the whole account model.

public function accountHistory()
{
Return $this->hasOneThrough(AccountHistory::class, Account::class);
}

Token guard token hashing:

With the token guard feature, you can now save the API tokens as SHA-256 hashes. Earlier the API’s token was stored as plain-text tokens which now has been improved. This has enhanced the security and the storing of the tokens generated.

Cache TTL:

The caching setting from minutes has now changed to seconds. This improvement is done for more precise and granular control for the expiration time. The feature conforms with the PSR-16 caching library standard.

Furthermore, remeber always to update your application caching to seconds.

// Laravel 5.7 - Store item for 5 minutes...
Cache::put('foo', 'bar', 10);

// Laravel 5.8 - Store item for 5 seconds...
Cache::put('foo', 'bar', 10);

Scheduler Timezone configuration:

This new version of Laravel allows you to customize the timezone for a listed task using the timezone method.

$schedule->command('run')
->daily()
->at('15:00')
->timezone('America/Chicago');

This was considered to be a hassle task in the earlier version as you had to do it for every scheduled task. But with this, you only now have to define a method called ScheduleTimezone in the app/Console/Kernel.php file.

The changes will then be attached to every scheduler in the list.

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

Artisan call improvements:

You can make Artisan command using Artisan::call method in this new version. The commands now can be passed as an array in a single string like given below:

Artisan::call('migrate:install --database=foo');

Artisan serve improvements:

The previous versions had the default port assigned in 8000 on which the application will run. And if any other command is running on this port it will probably fail to serve the new command.

The new version will scan the port up to 8009 allowing the developers to run multiple applications at the same time.

Mock/Spy Testing helper methods:

Mock testing will only help the developer in making the code cleaner and readable. The mock and the spy methods are added to the base Laravel test case class. This method will automatically bind the class to be mocked with the container.

public function testBasicTest()
{
$this->mock(TransactionService::class, function($mock){
$mock->shouldReceive('find')->once()->with(1)->andReturn(['id' => 1, 'name' => 'foo'])
});
}

Higher Order orWhere eloquent method:

In the previous versions, we would define the scoped query with “or” in closure callbacks. With the introduction of a “higher order” orWhere method, you can easily chain these scope queries collectively without using closures. For example:

$users = App\User::emailVerified()->orWhere->active()->get();

dotenv 3.0:

To manage your project’s new .env file, Laravel 5.8 will support new dotenv 3.0. This new dotenv 3.0 supports multiline strings and white space at the end of strings. This feature would be of great help where multiline API keys are required for security.

New error page templates:

New error pages are introduced with very minimalist designs which are suitable for a wide range of websites and apps. It is even possible to customize the error pages or import the previous designed error page.

There are many Laravel development companies for application development. This new version is a treasure trove of some of the amazing features. The content above has listed some of the notable improvements and features extended in the new Laravel 5.8 version.

Some of the features can smoothly be integrated into the websites to make it more functional and user-friendly.

To upgrade your running application to Laravel version 5.8, you can check out the upgrade guide and the release notes on the official site.