skip to Main Content

First of all I am new to Laravel framework, so please bear with me. I am creating an e-commerce website and created a registration page. The problem is whenever I fill the registration form and press register button the page will refresh but the data I filled will not be updated in MySQL database.

the registration file.
registration.blade.php
`

@extends('layouts.master')
@section('content')
@section('title', 'Register')

    <section class="login-page"> 
        <div class="login-form-box">
            <div class="login-title"> Register</div>
            <div class="login-form">
                <form action="{{route('register')}}" method="post">
                @csrf
                    <div class="field">
                        <label for="name">name</label>
                        <input type="text" id="name" name="name" placeholder="john" >

                    </div>

                    <div class="field">
                        <label for="email">email</label>
                        <input type="email" id="email" name="name" placeholder="[email protected]" >

                    </div>

                    <div class="field">
                        <label for="password">password</label>
                        <input type="password" id="password" name="password" placeholder="******" >

                    </div>

                    <div class="field">
                        <label for="passowrd_confirmation">confirm password</label>
                        <input type="password" id="password" name="passowrd_confirmation" placeholder="******" >

                    </div>

                    <div class="field">
                        <button type= "submit" class="btn btn-primary btn-block">Register</button>
                    </div>
                    

                </form>
            </div>
        </div>

    </section>
@endsection

`
Also, here the route file in laravel

web.php

<?php

use AppHttpControllersAuthController;
use AppHttpControllersPagesController;
use IlluminateSupportFacadesRoute;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', [PagesController::class, 'home'])->name('home');
Route::get('/cart', [PagesController::class, 'cart'])->name('cart');

//Auth
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'postRegister'])->name('register');

And here is the file for registration functions
AuthController.php
`

<?php

namespace AppHttpControllers;

use AppModelsUser;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesHash;

class AuthController extends Controller
{
    //show login page
    public function showLogin()
    {
        return view('pages.login');
    }

    //show register page
    public function showRegister()
    {
        return view('pages.register');
    }

     //register user
    public function postRegister(Request $request)
     {
        //validation
        $request->validate([
            'name' => 'required|min:3|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:8|confirmed'
        ]);


        //registration
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),

        ]);


        //login user
        Auth::login($user);

        return back()->with('success','succesfully Logged in');
        
     }
    

    
}

`
here is my Database in phpmyadmin after filling the form:
users database
I tried to use showRegister function in my AuthController.php in order to check if the data being filled can be seen in the webpage. However, I was not able to see anything.

2

Answers


  1. Display errors and old values

    First of all, I suggest you to update your view to display errors when using validation, and display old values:

    <div class="field">
      <label for="name">name</label>
      <input type="text" id="name" name="name" value="{{ old('name') }}" placeholder="john" >
      @error('name')
        <div>{{$message}}</div>
      @enderror
    </div>
    

    If the validation has no success, no changes will be made. Try to display if the validation has passed:

    public function postRegister(Request $request)
    {
      //validation
      $request->validate([
        'name' => 'required|min:3|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:8|confirmed'
      ]);
    
      dd("Validation passed!);
    }
    

    This will help you to find errors.

    Take a look at the official Laravel validation documentation

    Login or Signup to reply.
  2. Try to check validation errors messages in registration.blade.php file

    @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search