Home » What’s New in Laravel 7.0
whats-new-in-laravel-7
Laravel

What’s New in Laravel 7.0

Laravel 7 was officially released on the 3rd of March, 2020 and it includes multiple new features including Laravel Airlock, better routing speed, and many more features.

Before jumping straight into the new features, we would like to bring clarity to the latest updates starting from Laravel 6. These updates now follow semver and shall be releasing new major updates every six months. If you are looking for interview questions and answers related Laravel then you can visit here.

So, here’s what’s new in Laravel 7

  • Laravel Airlock
  • Custom Eloquent Casts
  • Route Caching Speed Improvements
  • Blade Component Tags & Other Improvements
  • HTTP Clients
  • The CORS Support
  • Fluent String Operations
  • New artisan Commands
  • Multiple Mail Driver

1. Laravel Airlock

It is mainly built by Taylor Otwell. Laravel Airlock provides an easy authentication system for mobile applications, single-page applications and simple token-based APIs applications. It will allow each and every user of your application to generate multiple API tokens for their account. All these tokens may be granted abilities which specify which actions the tokens are allowed to perform.

2. Custom Eloquent Casts

Currently, Laravel uses a variety of in-built and efficient cast types. However, there might be an occasional need to define your cast type. This can now be accomplished by defining one class that implements the CastsAttribute dashboard/interface. The classes that implement this particular interface must have a defined get and set methods. The Get method is used to transform a raw value from inside the database into one cast value. On the other hand, the set method is used to modify the cast value into a raw one which can then be easily stored inside any database.

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class Json implements CastsAttributes
{
    public function get($model, $key, $value, $attributes)
    {
       return json_decode($value, true);
    }
    public function set($model, $key, $value, $attributes)
    {
       return json_encode($value);
    }
}

Now we can use our custom eloquent cast in the model.

namespace App;
use App\Casts\Json;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
   protected $casts = [
      'extra' => Json::class,
   ];
}

3. Route Caching Speed Improvements

Laravel 7 will give 2x faster speed than Laravel 6 using route:cache. One more best feature is defined as Route Model binding. Using this feature you can bind your Model in your route.

Route::get('users/{user:slug}', function (App\User $user) {
return $user;
});

4. Blade Component Tags & Other Improvements

Laravel 7 allows you to define your own components and use them in your blade files like below:

Define Component

<?php 
namespace App\View\Components; 

use Illuminate\View\Component;

class Notify extends Component
{
       public $type;

       public function __construct($type)
       {

          $this->type = $type;
    }

       public function classForType()
    {
          return $this->type == 'success' ? 'notify-success' : 'notify-warning';
    }

       public function render()
    {
          return view('components.notify');
    }
}

Make Component Tag

<div class="notify {{ $classForType() }}">
{{ $heading }}{{ $slot }}
</div>

Using it in Blade Files

<x-notify type="error" class="mb-4">
<x-slot name="heading">
Alert content...
</x-slot>
Default slot content...
<x-notify>

Please consult the full Blade component documentation to learn about this feature.

5. HTTP Clients

Since Laravel 7, developers can quickly make outgoing HTTP requests to communicate with other web applications and mobile applications easily. This is possible due to Laravel’s expressive and minimal API based around the Guzzle HTTP client. Wrapper in Laravel, which is around Guzzle, focuses on the most commonly found use cases and more importantly, a good developer experience.

POST Request

use Illuminate\Support\Facades\Http;
$response = Http::post($url);
$response = Http::post($url, [
    'site' => 'bestinterviewquestion website',
]);

GET Request

$response = Http::get($url);
$response = Http::get($url,['name'=>'bestinterviewquestion']);

With Header

$response = Http::withHeaders(['xyz' => 'pqr'])->post($url, [ 
'baz' => 'qux', 
]);

Response

$response['name']
$response->body()
$response->json()
$response->status()
$response->ok()

6. The CORS Support

Laravel can automatically respond to CORS OPTION requests with values that you configure. All CORS settings may be configured in your CORS configuration file and OPTIONS requests will automatically be handled by the HandleCors middleware that is included by default in your global middleware stack.

7. Fluent String Operations

Laravel 7 now offers a more object-oriented, fluent string manipulation library built on top of these functions. Previously, llluminate\Support\str class was there, which provides a variety of helpful string manipulation functions.

But Now, you can create a fluent llluminate\Support\Striangable object using the Str::of method. A variety of methods may then be chained onto the object to manipulate the string:

return (string) Str::of('  Laravel Framework 6.x ')
                    ->trim()
                    ->replace('6.x', '7.x')
                    ->slug();

8. New artisan Commands

Besides the PHPUnit command, developers can now use the Artisan test command to run multiple tests. This test runner provides a beautiful and robust console User Experience while having more information regarding the tests running currently. In addition to this, the test runner will automatically stop when encountering the first test failure:

PHP artisan test

9. Multiple Mail Driver

Laravel 7 allows the configuration of multiple “mailers” for a single application. Each mailer configured within the mail configuration file may have its own options and even its own unique “transport”, allowing your application to use different email services to send certain email messages.

Tags

1 Comment

Click here to post a comment

42 − = 38

  • Hi! I simply want to offer you a big thumbs up for the great info you have got right here on this post. I will be returning to your website for more soon.