Laravel 5.8: what is new?

According to Laravel official documentation, version 5.8 comes with continued improvement in framework features.

Features:

  • Eloquent HasOneThrough Relationship
  • Auto-Discovery Of Model Policies
  • PSR-16 Cache Compliance
  • Token Guard Token Hashing
  • Improved Email Validation
  • Default Scheduler Timezone
  • Intermediate Table / Pivot Model Events
  • Artisan Call Improvements
  • Mock / Spy Testing Helper Methods
  • Eloquent Resource Key Preservation
  • Higher Order orWhere Eloquent Method
  • Artisan Serve Improvements
  • Blade File Mapping
  • DynamoDB Cache / Session Drivers
  • Carbon 2.0 Support
  • Pheanstalk 4.0 Support

Following are my favourite updates:

I will be discussing some of these updates, mostly I am dealing in my daily programming tasks.

1- Eloquent HasOneThrough Relationship:
In 5.8 Eloquent supports hasOneThrough relation type. Suppose User model hasOne Order model and Order model hasOne OrderStatusHistory.

HasOneThrough can be handy, code example:

/**
 * Get the order status history for the User.
 */
public function orderStatusHistory()
{
    return $this->hasOneThrough(OrderStatusHistory::class, Order::class);
}


2- Auto-Discovery Of Model Policies:
In Laravel 5.7 each model with authorization policy was required to be registered in application AuthServiceProvider:

/** 
 * The policy mappings for the application.
 * Laravel < 5.8
 * @var array
 */
protected $policies = [
    'App\User' => 'App\Policies\UserPolicy',
];

But now in Laravel 5.8, auto discovery of model policy introduced; only standard Laravel naming conventions are required.

PSR-16 Cache Compliance:
In order to provide compliance with PSR-16 caching standard, cache time-to-live has changed from minutes to seconds.

The putputManyaddremember and setDefaultCacheTime methods of the Illuminate\Cache\Repository class and its extended classes, as well as the put method of each cache store were updated with this changed behavior. 

Examples:

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

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

// Laravel 5.7 / 5.8 - Store item for 30 seconds...
Cache::put('foo', 'bar', now()->addSeconds(30));

3- Token Guard Token Hashing:
Laravel’s token guard, that provides basic API authentication now supports API token to be stored as SHA-256 hashes, review the full API authentication documentation

Hashing Tokens:
In order start storing API token encrypted with sha256 update config/auth.php with following settings, review the Generating Tokens

'api' => [
    'driver' => 'token',
    'provider' => 'users',
    'hash' => true,
],

// Implementation example
public function update(Request $request)
{
        $token = Str::random(60);
        $request->user()->forceFill([
            'api_token' => hash('sha256', $token),
        ])->save();

        return ['token' => $token];
}

4- Improved Email Validation:
Laravel 5.8 introduced improvements in email validation logic by adopting egulias/email-validator package. In previous versions of Laravel emails such as danyal@tæst.dk was considered a valid email.

5- Higher Order orWhere Eloquent Method:
Previous versions requires Closure callback when combining multiple Eloquent model scopes via (or) query operator. Take a look at examples below:

// Laravel < 5.8
// scopeNotBlacklisted and scopeNotBanned methods defined on the User model...
$users = App\User::notBlacklisted()->orWhere(function (Builder $query) {
    $query->notBanned();
})->get();
// Laravel 5.8
$users = App\User::notBlacklisted()->orWhere->notBanned()->get();

6- Artisan Serve Improvements:
In Laravel < 5.8 Artisan serve command serves you application on port 8000 default, but in case if other application is served at 8000 already second attempt would fail.
In Laravel 5.8 serve now will scan for available port and will serve you application next available port up to 8009.

7- Blade File Mapping:
When compiling Blade templates, Laravel now adds a comment to the top of the compiled file which contains the path to the original Blade template.

8- Carbon 2.0 Support:
Laravel 5.8 provides support for the ~2.0 release of the Carbon date manipulation library.

All in all Laravel 5.8 comes with quite useful improvements, take a full overview of change at Laravel 5.7 official documentation.

Author: Danyal
I'm skilled programmer with expertise in Vue.js/Nux.js for front-end development and PHP Laravel for back-end development. I excel in building APIs and services, and also have experience in web server setup & maintenance. My versatile skill set allows you to develop and maintain web applications effectively, from the user interface to the server-side functionality. I love coding with never ending learning attitude, thanks for visiting danya.dk