I'm starting in Laravel 5.5, and I have the following problem. I am creating the user login, I authenticate the user, but when I see it, I do not print it (Error: Undefined variable: users (View: C: \ xampp \ htdocs \ system_cine \ resources \ views \ dashboard. blade.php)) I tried to pass it with the function compact () and with (), but it does not work for me. I used the dd () function to print the user and it works for me. I have the problem at the moment of sending the object to view. I would like to know if anyone can give me any suggestions. Maybe I have the error elsewhere.
Class of the login Controller:
<?php
namespace App\Http\Controllers\Auth;
use Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class ControladorDeLogin extends Controller
{
protected $guard = 'usuarios';
public function __contruct(){
$this->middleware('guest',['only' => 'mostrarFormularioDeLogin']);
}
public function login(){
$credentials = $this->validate(request(),[
'email' =>'email|required|string',
'contrasenia' => 'required|string'
]);
if (Auth::attempt(['email'=> $credentials['email'] , 'password' => $credentials['contrasenia'] ] )){
$users = Auth::getUser();
//dd($users);
return redirect()->route('dashboard')->with('users',$users);
}
//Laravel convierte los arrays en formato JSON
return back()->withErrors(['email' => 'Estas credenciales no coinciden con nuestros registros','contrasenia' => 'No coincide'])
->withInput(request(['email']));
}
public function mostrarFormularioDeLogin(){
return view('login');
}
public function logout(){
Auth::logout();
return redirect('/');
}
}
The Dashboard view (where the user data would go)
@extends('layouts.app')
@section('users')
@section('content')
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-heading">
@foreach($users as $user)
<h1 class="panel-title">Bienvenido {{$user->email}}</h1>
@endforeach
</div>
<div class="panel-body">
<strong> Email:</strong>
</div>
<div class="panel-footer">
<form method="POST" action="{{ route('logout') }}">
{{ csrf_field() }}
<button class="btn btn-danger btn-xs btn-block"> Cerrar sesión</button>
</form>
</div>
</div>
</div>
@endsection
Dashboard driver
<?php
namespace App\Http\Controllers;
use App\Usuario;
use Illuminate\Http\Request;
class ControladorDelDashboard extends Controller
{
public function __contruct(){
$this->middleware('auth');
}
public function index(){
return view('dashboard');
}
}
Path declared in the web.php file:
Route::get('dashboard','ControladorDelDashboard@index')->name('dashboard');
Moment when I use the dd () function to print the user's object