Blog

Laravel 7 is now released

laravlbanr

Laravel 7 is now released that includes many new features including Laravel airlock, speed routing, better eloquent, blade component tags and many more.

These features are listed below:

1. Air Lock:

Air Lock is an official package for API authentication. Laravel airlocks provide an authentication system for single page application, mobile application and simple token-based API’s.

In Airlock feature it allows you to generate multiple API tokens for their account.

2. Custom Eloquent Cast:

Custom eloquent in Laravel is another feature. This feature will give you the ability to define your own cast types. Let’s understand this by an example

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class Json implements CastsAttributes
{
    public function getvalue($model, $key, $value, $attributes)
    {
        return json_decode($value, true);
    }

    public function setvalue($model, $key, $value, $attributes)
    {
        return json_encode($value);
    }
}

3. Blade Component Tags & Other Improvements:

Blade components allow ag-based rendering, attribute management, component classes. In summary, a component may now have an associated class which specifies the data it accepts.

Let’s take an example and assume we have

App\View\Components\AlertClass

namespace App\View\Components;

use Illuminate\View\Component;

class AlertExample extends Component
{
    /**
     * The alert type.
     *
     * @var string
     */
    public $type;

    /**
     * Create the component instance.
     *
     * @param  string  $type
     * @return void
     */
    public function __construct($type)
    {
        $this->type = $type;
    }

    /**
     * Get the class for the given alert type.
     *
     * @return string
     */
    public function classForType()
    {
        return $this->type == 'danger' ? 'alert-danger' : 'alert-warning';
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        return view('components.alert');
    }
}

And assume we have component’s Blade template has been defined like so:

/resources/views/components/alertclass.blade.php

{{ $heading }} {{ $slot }}

 

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

    
        Alert content...
    

    Default slot content...

4. HTTP Client:

Laravel now provides an expressive, minimal API around the Guzzle HTTP Client allowing you to quickly make an outgoing HTTP request to communicate with other web applications. For example, the client makes it a breeze to post and interface with JSON data

use Illuminate\Support\Facades\Http;
$response = Http::withHeaders([
    'X-First' => 'foo'
    'X-Second' => 'bar'
])->post('http://demo.com/users', [
    'name' => demo,
]);

return $response['id'];

5. Route Caching Speed Improvements:

Laravel 7 includes a new method of matching compiled, cached routes that have been cached using the route::cache artisan command. On large application like 1000 or more routes, these routes can result in 2x speed improvements in request per second on a simple “hello world” benchmark.

6. Query Time Casts:

Sometimes you need to cast the query while executing it such as selecting a row value from the table.

Let’s understand this with an example:

use App\Blog;
use App\User;

$users = User::select([
    'users.*',
    'last_updated_at' => Blog::selectRaw('MAX(created_at)')
            ->whereColumn('user_id', 'users.id')
])->get();

The last_updated_at attribute on the results of this query will be a raw string. It would be convenient if we could apply a date cast to this attribute when executing the query. To use this, we can write the code withCasts method provides in Laravel 7.

$users = User::select([
    'users.*',
    'last_updated_at' => Blog::selectRaw('MAX(created_at)')
            ->whereColumn('user_id', 'users.id')
])->withCasts([
    'last_updated_at' => 'date'
])->get();

7. Markdown Mail Template Improvements:

The default template now uses a fresh, more modern design based on the Tailwind CSS color palette. You can customize this template according to your application needs.

8. Multiple Mail Drivers:

Laravel 7 allows the configuration of multiple “mailers” for a single application. Each mailer configured within the mail configuration file. By default, Laravel uses mail configured as the default mailer in your mail configuration file. However, you can use the mailer method to send a message using a specific mailer configuration.

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

Conclusion:

Laravel 7 continues the improvements made in Laravel 6.x by introducing new features. Laravel 7 comes with many new features and provides the stability of your code. If you have any doubts regarding this article feel free to share your views on this article or leave a comment. We will get back to you soon.