Laravel 7

Introduction


On March 3, the Laravel team officially released Laravel 7.0. As stated in the Laravel support policy, this is one of the major updates. Although this release is not designated as LTS, it still introduces many great new features and fixes. The update will provide bug fixes until September 3, 2020, and security fixes for 1 year until March 3, 2021.


New Features in Laravel 7


X blade


Laravel 7 finalized the Blade template engine by adding completely new X features. This package provides an easy way to render custom HTML components in your Blade views.


Previously, you should have written like this:


@include('myAlert', ['type' => 'error', 'message' => $message])

Now, using x blade, it will look like this:


<my-alert type="error" :message="$message" />


Custom Stubs



In order to configure stub files, you need to publish them:


php artisan stub:publish

After running this command, a new directory will be added to your project.


Free String Operations


Previously, the llluminate \ Support \ str class provided many useful string functions. Laravel 7 now offers a more object-oriented, free string library built on top of these functions.


llluminate\Support\Striangable Str::of. :


return (string) Str::of('  Laravel Framework 6.x ')
                  ->trim()
                  ->replace('6.x', '7.x')
                  ->slug();

Zttp HTTP-


Zttp — Guzzle, . Zttp — PHP, Guzzle, .


Laravel Airlock


Laravel Airlock — , (SPA), API . C Airlock API . / , , .



Laravel 7 . (mailer), mail, “”, .


Eloquent (Custom Eloquent Casts)


, , get set. get , set , . , json :


    <?php
 
    namespace App\Casts;
     
    use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
     
    class Json implements CastsAttributes
    {
        /**
         * Cast the given value.
         *
         * @param  \Illuminate\Database\Eloquent\Model  $model
         * @param  string  $key
         * @param  mixed  $value
         * @param  array  $attributes
         * @return array
         */
        public function get($model, $key, $value, $attributes)
        {
            return json_decode($value, true);
        }
     
        /**
         * Prepare the given value for storage.
         *
         * @param  \Illuminate\Database\Eloquent\Model  $model
         * @param  string  $key
         * @param  array  $value
         * @param  array  $attributes
         * @return string
         */
        public function set($model, $key, $value, $attributes)
        {
            return json_encode($value);
        }
    }

(CORS)


Laravel CORS OPTION . CORS CORS, OPTIONS HandleCors, .



Sometimes you need to apply a transformation when executing a query, for example, when selecting a raw value from a table. Let's consider the following query:


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


Summary


I must say that this update makes Laravel 7 even easier and more enjoyable to use. So many long-awaited features are finally implemented and ready to use.


I think this is a great release, and I look forward to the next update. Hope you feel the same way!


All Articles