Laravel 7

介绍


3月3日,Laravel团队正式发布了Laravel 7.0。如Laravel支持政策所述,这是主要更新之一。尽管此版本未指定为LTS,但它仍引入了许多出色的新功能和修补程序。该更新将提供2020年9月3日之前的错误修复,以及2021年3月3日之前的1年安全修复。


Laravel 7的新功能


X刀片


Laravel 7通过添加全新的X功能最终确定了Blade模板引擎。该软件包提供了一种在Blade视图中呈现自定义HTML组件的简便方法。


以前,您应该这样写:


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

现在,使用x刀片,将如下所示:


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


自定义存根



为了配置存根文件,您需要发布它们:


php artisan stub:publish

运行此命令后,新目录将添加到您的项目中。


免费的字符串操作


以前,llluminate \ Support \ str类提供了许多有用的字符串函数。Laravel 7现在提供了一个基于这些功能的,面向对象的免费字符串库。


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



有时在执行查询时(例如,从表中选择原始值时)需要应用转换。让我们考虑以下查询:


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


摘要


我必须说,此更新使Laravel 7更加易于使用。如此众多期待已久的功能最终得以实现并可以使用。


我认为这是一个很棒的版本,我期待下一个更新。希望你有同样的感觉!


All Articles