lundi 23 mars 2020

Laravel canot register for new uses

I am creating a simple login function on my website with a registration feature. The registration feature is functional and users are appearing in the database. However,I try to register new use it cannot insert to database and it still at the same page and refresh data in form register. When I enter credentials, be it correct or wrong, the page would just reload with no error messages.

I've had this problem for a few days now and it really irritates me that I can't solve it. I'm at my wit's end so please help me!

This is my 'register.blade.php'

@extends('layouts.app')

   @section('content')
<div class="container">
<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header text-center h2"></div>

            <div class="card-body">
                <form method="POST" action="">
                    @csrf

                    <div class="form-group row">
                        <label for="text" class="col-md-4 col-form-label text-md-right"></label>

                        <div class="col-md-6">
                            <input id="fname" type="text" class="form-control" name="first_name" required autocomplete="first-name">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="text" class="col-md-4 col-form-label text-md-right"></label>

                        <div class="col-md-6">
                            <input id="fname" type="text" class="form-control" name="last_name" required autocomplete="last-name">
                        </div>
                    </div>




                    <div class="form-group row">
                        <label for="email" class="col-md-4 col-form-label text-md-right"></label>

                        <div class="col-md-6">
                            <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="" required autocomplete="email">

                            @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong></strong>
                                </span>
                            @enderror
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="password" class="col-md-4 col-form-label text-md-right"></label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">

                            @error('password')
                                <span class="invalid-feedback" role="alert">
                                    <strong></strong>
                                </span>
                            @enderror
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="password-confirm" class="col-md-4 col-form-label text-md-right"></label>

                        <div class="col-md-6">
                            <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="text" class="col-md-4 col-form-label text-md-right"></label>

                        <div class="col-md-6">
                            <input id="position" type="text" class="form-control" name="postion" required autocomplete="position">
                        </div>
                    </div>

                    <div class="form-group row">
                        <label for="address" class="col-md-4 col-form-label text-md-right"></label>

                        <div class="col-md-6">
                            <input id="address" type="address" class="form-control @error('address') is-invalid @enderror" name="address" value="" required autocomplete="address">

                            @error('address')
                                <span class="invalid-feedback" role="alert">
                                    <strong></strong>
                                </span>
                            @enderror
                        </div>
                    </div>


                    <div class="form-group row mb-0">
                        <div class="col-md-6 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

@endsection

This is my route web.php

  <?php

 use Illuminate\Support\Facades\Route;

  /*
   |--------------------------------------------------------------------------


    Route::get('/', function () {
    return view('auth.login');
   });

   Auth::routes();

   Route::get('/home', 'HomeController@index')->name('home');


   Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'Admin','middleware'=> 
    ['auth','admin']], function (){

    Route::get('dashboard','DashboardController@index')->name('dashboard');

   });
   Route::group(['as'=>'author.','prefix'=>'author','namespace'=>'Author','middleware'=> 
   ['auth','author']], function (){
   Route::get('dashboard','DashboardController@index')->name('dashboard');
   Route::post('register', 'RegisterController@register');  
   });

This is my RegisterController

   namespace App\Http\Controllers\Auth;

    use App\Http\Controllers\Controller;
   use App\Providers\RouteServiceProvider;
   use Illuminate\Support\Facades\Auth;
   use Illuminate\Foundation\Auth\RegistersUsers;
   use Illuminate\Support\Facades\Hash;
   use Illuminate\Support\Facades\Validator;

   class RegisterController extends Controller
   {
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
 protected $redirectTo;
/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    if(Auth::check() && Auth::user()->role->id == 1){
        $this -> redirectTo = route('admin.dashboard');
    }else {
        $this -> redirectTo = route('author.dashboard');
    }
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'first_name' => ['required', 'string', 'max:255'],
        'last_name' => ['required', 'string', 'max:255'],
        'address' => ['required', 'string', 'max:255'],
        'position' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'role_id' => 2,
        'first_name' => $data['name'],
        'last_name' => $data['last_name'],
        'address' => $data['address'],
        'email' => $data['email'],
        'positin' => $data['position'],
        'password' => Hash::make($data['password']),
    ]);   
  }    
 }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire