LoginSignup
43
31

More than 1 year has passed since last update.

Laravel 10🐿がリリースされたのだ🎉【Laravel 10 新機能】

Last updated at Posted at 2023-02-14

Laravel 10がきました!ぺちぱーとララベラー歓喜!新機能を紹介します
https://laravel.com/docs/10.x/releases

Laravel ぺなんと

if文なしで実行する処理を切り替えることができ、ABテストで便利です

image.png

    public function boot(): void
    {
        Feature::define('beta-testers', fn (User $user) => match (true) {
            $user->isBetaTester() => true,
            default => false,
        });
    }

Processファサード

シンフォニぃ使わなくてもコマンドを実行できます

use Illuminate\Support\Facades\Process;
 
$result = Process::run('ls -la');
 
$result->successful();
$result->failed();
$result->exitCode();
$result->output();
$result->errorOutput();
$result->throw();
$result->throwIf($condition);

戻り値に型宣言

PHPDocのreturnが不要になります

Laravel 9
image.png

Laravel 10
image.png

image.png

php artisan test --profile

PHPうにっと実行時に、プロファイルで実行時間が長いテストを見つけられます

image.png

パスワード生成関数

パスワード生成できます

use Illuminate\Support\Str;
 
$password = Str::password();
 
// 'EbJo2vE-AS:U,$%_gkrV4n,q~1xy/-_4'
 
$password = Str::password(12);
 
// 'qwuar>#V|i]N'

呼び出し可能なバリデーション

冗長なメソッドがいらないです

Laravel 9

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Titlecase implements Rule
{
    // commented constructor for brevity

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (ucwords($value) !== $value) {
            $message[] = 'Each word in :attribute must begin with a capital letter';

            return false;
        }

        return $this->messages === [];
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $message;
    }
}

Laravel 10

namespace App\Rules;

use Illuminate\Contracts\Validation\InvokableRule;

class Titlecase implements InvokableRule
{
    /**
     * Run the validation rule.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     * @return void
     */
    public function __invoke($attribute, $value, $fail)
    {
        if (ucwords($value) !== $value) {
            $fail('Each word in :attribute must begin with a capital letter');
        }
    }
}
routes/web.php
<?php

use App\Rules\Titlecase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::post('/', function (Request $request) {
    $request->validate([
        'title' => ['nullable', new Titlecase],
    ]);

    return view('welcome');
});

php artisan make:* のおもてなし

コントローラー名を入れ忘れても聞いてくれます

php artisan make:controller

image.png

laravel new example-application --pest

pestというテストフレームワークが使えます

まとめ

いかがでしたか?(🤛

3/23~ぺちぱーの会議があります。もしよければぜひ参加してほしいのだ

43
31
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
43
31