Slide 1

Slide 1 text

はじめての実践Laravelでつまづいた話 2019年10月7日 タピオカLT 2杯目

Slide 2

Slide 2 text

自己紹介

Slide 3

Slide 3 text

実現したいこと:ユーザーIDのみでログインする

Slide 4

Slide 4 text

しかし、Undefinedでエラーになる、、、

Slide 5

Slide 5 text

Laravelに認証機能を作る php artisan make:auth しかし、このままだと、パスワードが入力されていないエラーが出るので、 hiddenタグで値を渡すしかなかった。

Slide 6

Slide 6 text

ルーティング設定(Web.php) // web.php Route::post('/login', 'Auth\LoginController@authenticate');

Slide 7

Slide 7 text

ControllerのUse設定 // LoginController.php //以下追加 use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Models\User; use Illuminate\Validation\ValidationException;

Slide 8

Slide 8 text

Controller処理内容 // LoginController.php // 認証用のカスタム関数 (POST値を受け取る。ユーザー IDからIDのみを抽出) public function authenticate(Request $request) { $userid = $request->user_id; if (isset(User::where('user_id', $request->input( 'user_id'))->first()->id)) { // 認証成功時、ユーザー IDからIDを取り出す。 $user_id = User::where( 'user_id', request->input( 'user_id'))->first()->id; $id = Auth::loginUsingId($user_id); return redirect('/home'); } }

Slide 9

Slide 9 text

IDのみで取得する内容 User::where('user_id', $request->input('user_id'))->first()->id; $id = Auth::loginUsingId($user_id); 公式ドキュメント(英語) Authenticate A User By ID To log a user into the application by their ID, you may use the loginUsingId method. This method accepts the primary key of the user you wish to authenticate: Auth::loginUsingId(1); // Login and "remember" the given user... Auth::loginUsingId(1, true);

Slide 10

Slide 10 text

まとめ Laravelはコマンド含めて便利なことができるが、 一つずつの処理にどういう事があるのかドキュメントを見直す必 要があると感じた Eloquentについてのことがまだ曖昧なので理解度を深める

Slide 11

Slide 11 text

ありがとうございました