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...

6 Responses

  1. After I originally left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve 4 emails with the same comment. There has to be a way you are able to remove me from that service? Kudos!

  2. I couldn’t refrain from commenting. Perfectly written!

  3. I’m impressed, I have to admit. Rarely do I come across a blog that’s equally educative and entertaining, and let me tell you, you have hit the nail on the head. The issue is something which not enough men and women are speaking intelligently about. Now i’m very happy I found this in my hunt for something relating to this.

  4. gox scooter says:

    Greetings, I believe your site might be having browser compatibility problems. When I take a look at your website in Safari, it looks fine however when opening in I.E., it’s got some overlapping issues. I merely wanted to give you a quick heads up! Other than that, fantastic site!

  5. Serene Media says:

    There is certainly a lot to find out about this subject. I like all of the points you made.

  6. You’re so interesting! I do not believe I’ve read through a single thing like this before. So wonderful to discover someone with genuine thoughts on this topic. Seriously.. thanks for starting this up. This web site is one thing that’s needed on the web, someone with some originality!

Leave a Reply

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

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codeforbetter will use the information you provide on this form to be in touch with you and to provide updates and marketing.