How to Master New In Laravel 7 in 8 Simple Steps

Here are the new features in laravel 7 on March 3rd, 2020 including

  • Laravel Airlock
  • Custom Eloquent Casts
  • Blade Component Tags & Improvements
  • HTTP Client
  • Route Caching Speed Improvements
  • Fluent String Operations
  • Multiple Mail Drivers
  • Artisan test Command

Laravel Airlock

This new feature in laravel provide a  featherweight authentication system for

  • SPAs (Single page applications)
  • simple, token based APIs
  • mobile applications

this feature allows user to generate multiple Api tokens these token may be granted abilities/scop to perform the necessary action which are allowed to perform and validate requests.

Custom Eloquent Casts

In order to define your own cast type this feature is very helpful. In order to use this you must implements your class with CastsAttributes interface. Classes that implement this interface must define a get and set methods.

get  method is use to cast raw value from database to a cast valeu
st method is cast raw value to value insert in database (e.g json) 
<?php

namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class MyTestJson 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);
    }

}

?>

Once you have defined a custom cast type, you may attach it to a model attribute using its class name

<?php

namespace App;

use App\Casts\MyTestJson ;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $casts = [
        'options' => MyTestJson ::class,
    ];
}

Blade Component Tags & Improvements

New methods will available  App\View\Components\Alert  
the component may be managed using the automatically included $attribute variable, which is an attribute bag instance.

component may be rendered in another Blade view using the component’s tag

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

HTTP Client

In order to make quick request Http response through API’s now become easier with minimal code around the Guzzle HTTP client,  Example for post request.

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'h1' => 'foo',
    'h1' => 'bar'
])->post('http://api.com/user-create', [
    'name' => 'Suraj',
]);

return $response['id'];

For Http response

Http::fake([
    // Stub a JSON response for GitHub endpoints...
    'github.com/*' => Http::response(['foo' => 'bar'], 200, ['Headers']),
'google.com/*' => Http::response('Hello World', 200, ['Headers']),
]);

Fluent String Operations

Laravel 7 provide object-oriented, fluent string manipulation . You may create a fluent Illuminate\Support\Stringable

return (string) Str::of('  Suraj tester ')
                    ->trim()
                    ->replace('tester', 'Developer')
                    ->slug();

Multiple Mail Drivers

Now in laravel 7 you can configure multiple mail configuration and use as for multiple mail drivers stored in different mail configurations of your app. Laravel use the default mailer in mail configuration

Mail::mailer('test')
        ->to($request->user())
        ->send(new SendDetails($order));

Route Caching Speed Improvements

New method was introduced in laravel 7 as route:cache which is very useful in very large applications having 800 or more routes and results in 2X speed

Artisan test Command

The test command was contributed by Nuno Maduro.

In addition to the phpunit command, you may now use the test Artisan command to run your tests. 

Read More documentation on official website of Laravel for previous version you can read more docs here

You may also like...

Leave a Reply

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