質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

2660閲覧

Auth guard [admin] is not defined.

Hiroto_4431

総合スコア19

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2022/08/21 01:14

編集2022/08/21 01:49

前提

Laravelでマルチログイン機能を作っているのですが、エラーが出てしまい解決できなかったので、質問させていただきます。

環境

Laravel9
macos
Docker Sail

発生している問題・エラーメッセージ

Auth guard [admin] is not defined.

該当のソースコード

Route::get('/', function () { return view('admin.welcome'); }); Route::get('/dashboard', function () { return view('admin.dashboard'); })->middleware(['auth:admin'])->name('dashboard'); Route::middleware('guest')->group(function () { Route::get('register', [RegisteredUserController::class, 'create']) ->name('register'); Route::post('register', [RegisteredUserController::class, 'store']); Route::get('login', [AuthenticatedSessionController::class, 'create']) ->name('login'); Route::post('login', [AuthenticatedSessionController::class, 'store']); }); Route::middleware('auth:admin')->group(function () { Route::post('logout', [AuthenticatedSessionController::class, 'destroy']) ->name('logout'); });

RouteServiceProvider.php

1<?php 2 3namespace App\Providers; 4 5use Illuminate\Cache\RateLimiting\Limit; 6use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 7use Illuminate\Http\Request; 8use Illuminate\Support\Facades\RateLimiter; 9use Illuminate\Support\Facades\Route; 10 11class RouteServiceProvider extends ServiceProvider 12{ 13 /** 14 * The path to the "home" route for your application. 15 * 16 * Typically, users are redirected here after authentication. 17 * 18 * @var string 19 */ 20 public const HOME = '/dashboard'; 21 public const COMPANY_HOME = '/company/dashboard'; 22 public const ADMIN_HOME = '/admin/dashboard'; 23 24 /** 25 * The controller namespace for the application. 26 * 27 * When present, controller route declarations will automatically be prefixed with this namespace. 28 * 29 * @var string|null 30 */ 31 // protected $namespace = 'App\\Http\\Controllers'; 32 33 /** 34 * Define your route model bindings, pattern filters, and other route configuration. 35 * 36 * @return void 37 */ 38 public function boot() 39 { 40 $this->configureRateLimiting(); 41 42 $this->routes(function () { 43 Route::prefix('api') 44 ->middleware('api') 45 ->namespace($this->namespace) 46 ->group(base_path('routes/api.php')); 47 48 Route::prefix('admin') 49 ->as('admin.') 50 ->middleware('web') 51 ->namespace($this->namespace) 52 ->group(base_path('routes/admin.php')); 53 54 Route::prefix('company') 55 ->as('company.') 56 ->middleware('web') 57 ->namespace($this->namespace) 58 ->group(base_path('routes/company.php')); 59 60 Route::prefix('/') 61 ->as('user.') 62 ->middleware('web') 63 ->namespace($this->namespace) 64 ->group(base_path('routes/web.php')); 65 }); 66 } 67 68 /** 69 * Configure the rate limiters for the application. 70 * 71 * @return void 72 */ 73 protected function configureRateLimiting() 74 { 75 RateLimiter::for('api', function (Request $request) { 76 return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); 77 }); 78 } 79} 80

config/auth.php

1<?php 2 3return [ 4 5 /* 6 |-------------------------------------------------------------------------- 7 | Authentication Defaults 8 |-------------------------------------------------------------------------- 9 | 10 | This option controls the default authentication "guard" and password 11 | reset options for your application. You may change these defaults 12 | as required, but they're a perfect start for most applications. 13 | 14 */ 15 16 'defaults' => [ 17 'guard' => 'users', 18 'passwords' => 'users', 19 ], 20 21 /* 22 |-------------------------------------------------------------------------- 23 | Authentication Guards 24 |-------------------------------------------------------------------------- 25 | 26 | Next, you may define every authentication guard for your application. 27 | Of course, a great default configuration has been defined for you 28 | here which uses session storage and the Eloquent user provider. 29 | 30 | All authentication drivers have a user provider. This defines how the 31 | users are actually retrieved out of your database or other storage 32 | mechanisms used by this application to persist your user's data. 33 | 34 | Supported: "session" 35 | 36 */ 37 38 'guards' => [ 39 'web' => [ 40 'driver' => 'session', 41 'provider' => 'users', 42 ], 43 44 'users' => [ 45 'driver' => 'session', 46 'provider' => 'users', 47 ], 48 'companies' => [ 49 'driver' => 'session', 50 'provider' => 'companies', 51 ], 52 'admins' => [ 53 'driver' => 'session', 54 'provider' => 'admins', 55 ], 56 'api' => [ 57 'driver' => 'token', 58 'provider' => 'users', 59 'hash' => false, 60 ], 61 ], 62 63 /* 64 |-------------------------------------------------------------------------- 65 | User Providers 66 |-------------------------------------------------------------------------- 67 | 68 | All authentication drivers have a user provider. This defines how the 69 | users are actually retrieved out of your database or other storage 70 | mechanisms used by this application to persist your user's data. 71 | 72 | If you have multiple user tables or models you may configure multiple 73 | sources which represent each model / table. These sources may then 74 | be assigned to any extra authentication guards you have defined. 75 | 76 | Supported: "database", "eloquent" 77 | 78 */ 79 80 'providers' => [ 81 'users' => [ 82 'driver' => 'eloquent', 83 'model' => App\Models\User::class, 84 ], 85 86 'companies' => [ 87 'driver' => 'eloquent', 88 'model' => App\Models\Company::class, 89 ], 90 91 'admins' => [ 92 'driver' => 'eloquent', 93 'model' => App\Models\Admin::class, 94 ], 95 96 // 'users' => [ 97 // 'driver' => 'database', 98 // 'table' => 'users', 99 // ], 100 ], 101 102 /* 103 |-------------------------------------------------------------------------- 104 | Resetting Passwords 105 |-------------------------------------------------------------------------- 106 | 107 | You may specify multiple password reset configurations if you have more 108 | than one user table or model in the application and you want to have 109 | separate password reset settings based on the specific user types. 110 | 111 | The expire time is the number of minutes that each reset token will be 112 | considered valid. This security feature keeps tokens short-lived so 113 | they have less time to be guessed. You may change this as needed. 114 | 115 */ 116 117 'passwords' => [ 118 'users' => [ 119 'provider' => 'users', 120 'table' => 'password_resets', 121 'expire' => 60, 122 'throttle' => 60, 123 ], 124 125 'companies' => [ 126 'provider' => 'companies', 127 'table' => 'company_password_resets', 128 'expire' => 60, 129 'throttle' => 60, 130 ], 131 132 'admins' => [ 133 'provider' => 'admins', 134 'table' => 'admin_password_resets', 135 'expire' => 60, 136 'throttle' => 60, 137 ], 138 ], 139 140 /* 141 |-------------------------------------------------------------------------- 142 | Password Confirmation Timeout 143 |-------------------------------------------------------------------------- 144 | 145 | Here you may define the amount of seconds before a password confirmation 146 | times out and the user is prompted to re-enter their password via the 147 | confirmation screen. By default, the timeout lasts for three hours. 148 | 149 */ 150 151 'password_timeout' => 10800, 152 153]; 154

Middleware/Authenticate.php

1<?php 2 3namespace App\Http\Middleware; 4 5use Illuminate\Auth\Middleware\Authenticate as Middleware; 6use Illuminate\Support\Facades\Route; 7 8class Authenticate extends Middleware 9{ 10 protected $user_route = 'user.login'; 11 protected $company_route = 'company.login'; 12 protected $admin_route = 'admin.login'; 13 14 /** 15 * Get the path the user should be redirected to when they are not authenticated. 16 * 17 * @param \Illuminate\Http\Request $request 18 * @return string|null 19 */ 20 protected function redirectTo($request) 21 { 22 if (!$request->expectsJson()) { 23 if (Route::is('company.*')) { 24 return route($this->company_route); 25 } elseif (Route::is('admin.*')) { 26 return route($this->admin_route); 27 } else { 28 return route($this->user_route); 29 } 30 } 31 } 32} 33

試したこと

キャッシュのクリアや再構築もしましたが、問題解決に至りませんでした。

追記

イメージ説明
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2022/08/21 01:38

どの行でそのエラーが出るんでしょうか
Hiroto_4431

2022/08/21 01:49

エラー画面の写真を追加させていただきました
guest

回答1

0

ベストアンサー

php

1Route::get('/dashboard', function () { 2 return view('admin.dashboard'); 3})->middleware(['auth:admin'])->name('dashboard'); 4 ↑adminではなくadminsにしてみてはどうでしょうか? 5 6~(中略)~ 7 8Route::middleware('auth:admin')->group(function () { 9 ↑adminではなくadminsにしてみてはどうでしょうか? 10 Route::post('logout', [AuthenticatedSessionController::class, 'destroy']) 11 ->name('logout'); 12}); 13

投稿2022/08/21 02:03

編集2022/08/21 02:06
mineralwater

総合スコア289

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問