##前提、実現したい事
<環境>
OS : macOS Big Sur 11.4
MAMP
Laravel : 6.2
<実現したい事>
ユーザー登録で2モデル作成したい(一般者と管理者のように)
##該当のソースコード
php
1Route::get('/login/producer', 'Auth\LoginController@showProducerLoginForm'); 2Route::get('/register/producer', 'Auth\RegisterController@showProducerRegisterForm'); 3 4Route::post('/login/producer', 'Auth\LoginController@producerLogin'); 5Route::post('/register/producer', 'Auth\RegisterController@createProduce'); 6 7Route::view('/producer', 'producer')->middleware('auth:producer')->name('producer');
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\Providers\RouteServiceProvider; use App\User; use App\Producer; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; //別枠でのユーザー登録で追加 use Illuminate\Http\Request; use Illuminate\Support\Facades\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'); $this->middleware('guest:producer'); } /** * 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', 'min:3', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8',], ]); } /** * 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']), ]); } //のクラス自身と継承クラスからアクセス可能 protected function producerValidator(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:admins'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); } public function showProducerRegisterForm() { return view('auth.register', ['authgroup' => 'producer']); } protected function createProducer(Request $request) { $this->producerValidator($request->all())->validate(); $producer = Producer::create([ 'name' => $request['name'], 'email' => $request['email'], 'password' => Hash::make($request['password']), ]); return redirect()->intended('login/producer'); } }
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ], 'producer' => [ 'driver' => 'session', 'provider' => 'producers', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], 'producers' => [ 'driver' => 'eloquent', 'model' => App\Producer::class, ], ],
必要情報ありましたら、教えてください。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/21 05:09
2021/06/21 05:21
2021/06/21 05:22
2021/06/21 05:37
2021/06/21 07:59
2021/06/21 08:00
2021/06/21 08:43
2021/06/21 08:49