0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP備忘録:Laravelで新規登録機能を構築するまでのステップを整理してみた

Posted at

はじめに

Laravel で新規登録機能を実装する方法について、手順とサンプルコードをまとめました。個人の備忘録程度の走り書きとなっておりますが、温かい目で見守っていただければ幸いです。

書こうと思ったきっかけ

個人的に PHP のキャッチアップをしたくて Laravel を学習中です。その過程で、簡単なユーザー登録機能を実装しましたので、忘れないように備忘録として記録します。

内容

ステップ一覧(新規登録機能)

① users テーブルの確認

初期のマイグレーションで含まれています。確認コマンド:

docker compose exec php-app php artisan migrate:status

create_users_table が [Ran] でなければ以下でマイグレーションを実行:

docker compose exec php-app php artisan migrate

② User モデルを確認

app/Models/User.php にあり、編集はまだ不要。

③ AuthController の作成

docker compose exec php-app php artisan make:controller AuthController

④ コントローラの編集

app/Http/Controllers/AuthController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    public function showRegisterForm()
    {
        return view('auth.register');
    }

    public function register(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);

        User::create([
            'name' => $validated['name'],
            'email' => $validated['email'],
            'password' => Hash::make($validated['password']),
        ]);

        return redirect('/register')->with('success', 'ユーザー登録が完了しました');
    }
}

⑤ ルート設定を追加

routes/web.php

use App\Http\Controllers\AuthController;

Route::get('/register', [AuthController::class, 'showRegisterForm']);
Route::post('/register', [AuthController::class, 'register']);

⑥ Blade テンプレート作成

resources/views/auth/register.blade.php

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>ユーザー登録</title>
</head>
<body>
    <h1>ユーザー登録フォーム</h1>

    @if (session('success'))
        <p style="color:green">{{ session('success') }}</p>
    @endif

    @if ($errors->any())
        <ul style="color:red;">
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    @endif

    <form method="POST" action="/register">
        @csrf
        <label>名前: <input type="text" name="name" value="{{ old('name') }}"></label><br>
        <label>メールアドレス: <input type="email" name="email" value="{{ old('email') }}"></label><br>
        <label>パスワード: <input type="password" name="password"></label><br>
        <label>パスワード(確認): <input type="password" name="password_confirmation"></label><br>
        <button type="submit">登録</button>
    </form>
</body>
</html>

アクセス確認

ブラウザで次の URL にアクセス:

http://localhost:8080/register

実際の画面

Screenshot 2025-05-10 at 21.29.43.png

PW制御画面

Screenshot 2025-05-10 at 21.31.08.png

Screenshot 2025-05-10 at 21.34.30.png

DB登録後画面

Screenshot 2025-05-10 at 21.52.16.png

まとめ

Laravel ではコマンドとディレクトリ構成が整理されており、登録機能の追加も簡単でした。

今回のステップを覚えておけば、今後のログイン機能やバリデーション処理にも応用できると思いました...!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?