Warning: This article was written in 2020, the content might be out of date.

Laravel 7

Categories: laravel

Laravel 7 is almost here, after watching the Laracon Online talk, I am very excited about this release. The new features I can relate to are HTTP Client, Fluent String Operations, and artisan test command. There are more good stuff such as Airlock, Custom eloquent casts, and the automatic scoping in route model are awesome to checkout too.

Since HTTP Client, Fluent String Operations and artisan test are the one will make positive impact to me personally immediately, I will take a look at them here first.

HTTP Client

Up until Laravel 7, if you need to make a HTTP request programmatically, Guzzle is the best available options. Now, Laravel provideds a minimal API wrap around Guzzle to make things easier. e.g.

Route::get('/http-client', function() {
    $response = Http::get('https://freegeoip.app/json/');
    $data = $response->json();
    echo $data['country_code']; // US
});

In Laravel, it even automatically detect the response as JSON. So you can just do

Route::get('/http-client', function() {
    echo Http::get('https://freegeoip.app/json/')['country_code'];
});

See more about HTTP Client.

Fluent Strings

The Strings helpers are wonderful. In Laravel 7, you can now chain it. e.g. If I want to convert Thank you, Laravel 6 to Hello, Laravel 7, I can do it in single line.

// Pre Laravel 7
$string = 'Thank you, Laravel 6';
$string = Str::replaceFirst('Thank you', 'Hello', $string);
$string = Str::replaceLast('6', '7', $string);
echo $string;

$fluentString = 'Thank you, Laravel 6';
$fluentString = Str::of($fluentString)
                    ->replaceFirst('Thank you', 'Hello')
                    ->replaceLast('6', '7');
echo $fluentString;

Artisan test

I think probably definitely love artisan test.

I used to have an alias p to vendor/bin/phpunit. Now I will just do artisan test starting with Laravel 7.

laravel-7-artisan-test

It is just a lot cleaner.

Thank you, Taylor and the Laravel team.

Next | Previous