php laravel code for user registration
Posted by Superadmin on September 06 2024 05:34:30

Sure! Here’s a step-by-step guide to creating a user registration system in Laravel:

1. Install Laravel

First, make sure you have Composer installed. Then, create a new Laravel project:

composer create-project --prefer-dist laravel/laravel user-registration
cd user-registration

2. Set Up Database

Update your .env file with your database credentials:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_db_username
DB_PASSWORD=your_db_password

3. Create User Model and Migration

Laravel comes with a User model and migration by default. You can modify the migration file if needed:

PHP
// database/migrations/xxxx_xx_xx_create_users_table.php
Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Run the migration:

php artisan migrate

4. Create Registration Controller

Generate a registration controller:

php artisan make:controller Auth/RegisterController

Update the controller to handle registration:

PHP
// app/Http/Controllers/Auth/RegisterController.php
namespace App\Http\Controllers\Auth;

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

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

    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        $user = $this->create($request->all());

        // Optionally, log the user in after registration
        // Auth::login($user);

        return redirect()->route('home');
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

5. Create Registration View

Create a registration view:

HTML
<!-- resources/views/auth/register.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
</head>
<body>
    <h2>Register</h2>
    <form method="POST" action="{{ route('register') }}">
        @csrf
        <div>
            <label for="name">Name</label>
            <input type="text" id="name" name="name" required>
        </div>
        <div>
            <label for="email">Email</label>
            <input type="email" id="email" name="email" required>
        </div>
        <div>
            <label for="password">Password</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div>
            <label for="password_confirmation">Confirm Password</label>
            <input type="password" id="password_confirmation" name="password_confirmation" required>
        </div>
        <button type="submit">Register</button>
    </form>
</body>
</html>

6. Define Routes

Add routes for registration in your web.php file:

PHP
// routes/web.php
use App\Http\Controllers\Auth\RegisterController;

Route::get('register', [RegisterController::class, 'showRegistrationForm'])->name('register');
Route::post('register', [RegisterController::class, 'register']);

7. Test Your Registration

Start your Laravel development server:

php artisan serve

Visit http://127.0.0.1:8000/register to see your registration form and test the registration process.

This should give you a basic user registration system in Laravel. If you need more advanced features, you can refer to the Laravel documentation or other detailed tutorials12.

Feel free to ask if you have any questions or need further assistance! 😊

1Laravel 9 Auth Login and Registration with Username or Email 2Laravel 10 Custom Login and Registration: A Comprehensive Guide