バージョン:Laravel 6.20.32
Laravelをインストールして、下記のテーブルのマイグレーションがデフォルトで作成される。
- users
- password_resets
- failed_jobs
これらのテーブル名を変更した際に修正する必要のある箇所について。
usersのモデル、コントローラ、マイグレーションの修正、作成している場合はファクトリ、シーダの修正、ルーティングに記述している場合はルーティングも修正が必要。
password_resetsとfailed_jobsのマイグレーションの修正が必要。(モデル名、テーブル名を変更する際の修正箇所についてはこちらも参照。)
加えて下記の対応が必要。
- config/auth.phpの修正
- RegisterController.phpの修正
config/auth.phpの修正
下記の例は、モデル名を『DUser』、テーブル名を『d_users』、『d_password_resets』に変更した場合のもの。
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'd_users', // 'passwords'を下記の'passwords'にて修正した'd_users'に変更!!
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'd_users', // 'provider'を下記の'providers'にて修正した'd_users'に変更!!
],
'api' => [
'driver' => 'token',
'provider' => 'd_users', // 'provider'を下記の'providers'にて修正した'd_users'に変更!!
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'd_users' => [ // usersからd_usersに変更!!
'driver' => 'eloquent',
'model' => App\DUser::class, // モデル名を変更!!
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'd_users' => [ // usersからd_usersに変更!!
'provider' => 'd_users', // 'provider'を上記の'providers'にて修正した'd_users'に変更!!
'table' => 'd_password_resets', // テーブル名を変更!!
'expire' => 60,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];
RegisterController.phpの修正
モデル名と、バリデーション部分を修正する。
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\DUser; // モデル名を変更!!
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:d_users'], // バリデーションも変更!!
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\DUser
*/
protected function create(array $data)
{
return DUser::create([ // モデル名を変更!!
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
}