I’m a beginner, learning Laravel. I was trying to create a web page which will show me my login Info(like email and password) once I log in. In my controller part I passed values using route parameter, but when I call it in blade, it shows
ErrorException Undefined variable $credentials (View: …..new projectNewLearnresourcesviewswelcome.blade.php)
My database has users table( email and password columns (which will be shown only), along with name,id, timestamps).
my LoginController
public function loginProcess(Request $request) { $this->validate($request, [ 'email' => 'required|email', 'password' => 'required|min:4', ]); $credentials = $request->except(['_token']); if (auth()->attempt($credentials)) { return redirect()->route('welcome', [$credentials->email]); } else{ $this->setErrorMessage('Invalid!'); return redirect()->back(); }
my routes
Route::get('/', function () { return view('login');}); Route::view('/welcome', 'welcome')->middleware('auth')->name('welcome'); Route::post('/login', [AppHttpControllersLoginController::class, 'loginProcess'])->middleware('guest')->name('login'); Route::get('/login', [AppHttpControllersLoginController::class, 'loginProcess'])->middleware('guest');
my welcome.blade.php
@extends('master') @section('contents') @include('partials.jumbotron') @stop @section('content') <h3 class="pb-3 mb-4 font-italic border-bottom"> {{$credentials->email}} </h3> <div class="blog-post"> <h2 class="blog-post-title">Sample blog post</h2> </div> <nav class="blog-pagination"> <a class="btn btn-outline-primary" href="#">Older</a> <a class="btn btn-outline-secondary disabled" href="#">Newer</a> </nav> @stop
Where am I making the mistake? Thanks in advance.
Advertisement
Answer
You are making it pretty hard on yourself. Let us start by changing
Route::view('/welcome', 'welcome')->middleware('auth')->name('welcome');
to
Route::get('/welcome', function() { $user = auth()->user(); return view('welcome', compact('user')); })->middleware('auth')->name('welcome');
Now in login process simply change
if (auth()->attempt($credentials)) { return redirect()->route('welcome', [$credentials->email]); }
to
if (auth()->attempt($credentials)) { return redirect()->route('welcome'); }
Now inside your blade file you can simple use
{{ $user->email }}