Reputation: 417
Working on a new project, I used the laravel auth scaffold, but I edited the views such as login, registration etc, layout to look for aesthetically pleasing. In doing so the form once submitted seems to be working however when refreshing the database no record is sent through.
No idea what I am missing here. I will include the code below. Hopefully, you can point me in the right direction.
HTML
@extends('layouts.app')
@section('content')
<div class="flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
<div class="max-w-md w-full">
<div>
<h2 class="mt-6 text-center text-3xl leading-9 font-extrabold text-gray-900">
Register
</h2>
</div>
<form class="mt-8" action="/register" method="POST">
@csrf
<input type="hidden" name="remember" value="true" />
<div class="rounded-md shadow-sm">
<div>
<input aria-label="Name" name="name" type='text' required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5" placeholder="Full Name" />
</div>
<div class="-mt-px">
<input aria-label="Email address" name="email" type="email" required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5" placeholder="Email address" />
</div>
<div class="-mt-px">
<input aria-label="Password" name="password" type="password" required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5" placeholder="Password" />
</div>
<div class="-mt-px">
<input aria-label="Password-confirm" name="password_confirmation" type="password" required class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5" placeholder="Confirm Password" />
</div>
<div class="mt-6">
<button type="submit" class="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm leading-5 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition duration-150 ease-in-out">
<span class="absolute left-0 inset-y-0 flex items-center pl-3">
<svg class="h-5 w-5 text-indigo-500 group-hover:text-indigo-400 transition ease-in-out duration-150" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z" clip-rule="evenodd" />
</svg>
</span>
Register
</button>
</div>
</form>
</div>
@endsection
Register Controller
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
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 = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$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, [
'name' => ['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([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}
Web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('home');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/share', 'HomeController@index')->name('share');
Route::get('/about', 'HomeController@index')->name('home');
Route::get('/contact', 'HomeController@index')->name('home');
Route::get('/register/create', 'RegisterController@create')->name('register');
Upvotes: 1
Views: 1149
Reputation: 825
When the form is sent to your server from the browser there's no POST
route to /register
defined in routes/web.php
for it to arrive at, but the form uses POST
and that route. Try the changes below.
HTML:
<form class="mt-8" action="/register/create" method="POST">
web.php
Route::post('/register/create', 'Auth\RegisterController@create')->name('register');
Secondly, the Register
controller's create()
method is not receiving the Request
object from the route, rather an unspecified array named $data
.
Add this to the use
statements at the top of your controller
use Illuminate\Http\Request;
Then change the create(array $data)
method like below, so it injects the Request
object as $request
instead of array $data
. Finally, you should use the framework and call the all()
method on the $request
instance to create the record in the DB.
/**
* Create a new user instance after a valid registration.
*
* @param Request $request
* @return \App\User
*/
protected function create(Request $request)
{
return User::create($request->all());
}
Upvotes: 1
Reputation: 647
Maybe you are not storing the model in the DB.
Try to dump and die -> dd()
step by step the data in your create()
method to know if you are receiving the data from the view. Something like:
protected function create(array $data)
{
// first if the $data is not empty
dd($data);
// then if the user is created
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
dd($user);
return $user;
}
If the array is not empty but the $user is, the problem could come from the DB which means you are not connecting to the DB successfully.
Revise your .env
file with the DB connection values.
Upvotes: 1