I know this post is a bit on the older side but I had a similar issue with Laravel 7. I think Zane's answer above should be the accepted answer. Just to elaborate on that use the following steps. This should be done after installing the auth scaffolding with composer and php artisan. Note: I am by no means a Laravel pro. If there are any issues with my code PLEASE let me know. The more I learn the better I can be.
Prepare the User Model
Ensure your App\User model implements Illuminate\Contracts\Auth\MustVerifyEmail:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
// ...
}
Setup Routes
In routes\web.php change this:
Auth::routes();
To this:
Auth::routes(['verify'=>true]);
After that you can specify which routes need a verified email address by either using middleware directly on a route like this:
Route::get('/profile','ProfileController@index')->middleware('verified');
or you can do it in the controller's constructor function like this:
public function __construct()
{
$this->middleware(['auth','verified']);
}
Modified Register Controller
I'm using the following register controller code. Note the register function contains a call to the following:
event(new Registered($user));
This was the key to getting the initial registration email to send.
Register Controller
Keep in mind this controller was designed to work with a primarily ajax site, thus why the register function returns a json response.
<?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;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
use Auth;
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, [
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'phone_number' => ['required', 'numeric', 'min:10'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'password_confirmation'=> ['required', 'string'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
$user=User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'phone_number' => $data['phone_number'],
'password' => Hash::make($data['password']),
]);
return $user;
}
/**
* Execute registration and login the user
*
* @param array $request
* @return response
*/
public function register(Request $request) {
$validation = $this->validator($request->all());
if ($validation->fails()) {
return response()->json($validation->errors(),422);
}
else{
$user = $this->create($request->all());
event(new Registered($user));
Auth::login($user);
if (Auth::user()){
return response()->json(['success'=>'Registration Successful']);
}
}
}
}