0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravel breeze による ログインページの作成

0
Posted at

開発環境

laravel 10
tailwindcss

目標

laravel/breezeを使用してログインページの作成

1.breezeパッケージの導入

composer require laravel/breeze --dev

php artisan breeze:install blade

2.usersテーブルを作成

php artisan migrate

3.viewの作成

    <div class="mx-auto mt-10">

        // ログインに失敗した際のメッセージを表示
        @error('login')
            <p>{{ $message }}</p>
        @enderror
        
        <form method="post" action>
            @csrf

            // フォームのデザインをいい感じにしてcomponentsでまとめた
            <x-form-label title="メールアドレス" name="email"/>
            <x-form-label title="パスワード" name="password"/>
            
            <button class="bg-gray-200 rounded-md px-2 mt-4">ログイン</button>
        </form>
    </div>

ControllerでPOST処理

    // ちょっとしたバリデーションをLoginFormRequestに記述
    public function login(LoginFormRequest $request)
    {
       $credentials = $request->only("email", "password");

        // ログインに成功した場合
       if(Auth::attempt(($credentials))){
            return redirect()->route("mypage");
            
        // ログインに失敗した場合
       }else{
            return redirect()
                ->route("top")
                ->withErrors([
                    "login" => "メールアドレスまたはパスワードが間違っています。"
                ]);
        }  

参考資料

【Laravel8初級】ログイン機能作成(全7回)
https://www.youtube.com/playlist?list=PLCyDm9NTxdhLV5LFfCoRykyHW8Zw91YJB

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?