What's New in Laravel 7
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
- The Laravel Airlock
- Custom Eloquent Casts
- Blade Component Tags & Other Improvements
- HTTP Clients
- Route Caching Speed Improvements
- The CORS Support
- MySQL 8+ Database Queue Enhancements
- Artisan test Command
- Multiple Mail Drivers
1. The Laravel Airlock
The laravel airlock will be used to provide an authentication system for SPAs (Single Page Application), mobile apps, and also, ticket-based APIs. Laravel Airlock allows all users within your application to generate multiple API tickets in their account. These tokens are enabled with abilities/scopes to specify some particular actions which the tickets are permitted 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. Blade Component Tags & Other Improvements
In Laravel, the Blade components are always used to enable rendering, which is tag-based, efficient attribute management, multiple component classes, an inline view into components, and much more. Any component can now have an associated class which will be used to specify the data which it accepts readily. All the public properties and other methods that are defined on any component class will be automatically made available towards the specific component view. Besides this, any additional HTML attributes which are specified on the component can also be managed using the $attribute variable, which is an automatically included variable acting as an attribute bag instance.
Define Component
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' ? ‘success’ : 'warning';
}
public function render()
{
return view('components.notify');
}
}
Make Component Tag
{{ $heading }}{{ $slot }}
Using it in Blade files
Alert content here ...
Default slot content here ...
4. 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(['name' => 'bestinterviewquestion'])->post($url, [
'baz' => 'qux',
]);
Response
$response['name']
$response->body()
$response->json()
$response->status()
$response->ok()
5. Route Caching Speed Improvements
Laravel 7 comes with a new and efficient method of matching compiled and cached routes which are cached through the route:cache Artisan command. On some more extensive applications, (applications with 800 or more routes), the above improvements could result in more than 2x speed improvement regarding requests per second on a fundamental "Hello World" code. Moreover, no changes to the application are required.
Route::get('users/{user:slug}', function (App\User $users) {
return $users;
});
6. The CORS Support
In Laravel 7, there is first-party support used for configuring CORS, (Cross-Origin Resource Sharing) requests handling using the integrated Laravel CORS package.
7. MySQL 8+ Database Queue Enhancements
In the previous updates of Laravel, database queues were not considered flexible enough on a production level due to issues and other deadlocks. However, in Laravel 7, there are improvements to these applications using MySQL 8 or above as their database-backed queues. Using the FOR UPDATE SKIP LOCKED clause, other SQL enhancements, now, the database driver can safely be used in applications with higher volume production.
8. Artisan test Command
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
All the arguments that can be passed to the PHPUnit command can also be passed on to the Artisan test command using the following: