Laravel 5.5 how to print an object in the view?

0

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

    
asked by Federico Nery 26.09.2018 в 18:23
source

1 answer

1

If you want to print some data of the user ALREADY LOGGED , you can use this in your view (any view) while you are already logged in, otherwise it will return exception:

{{ Auth::user()->name }}

You can change "name" to any field in your Users table, such as: email.

If you want to see a specific user, you can consult BD from your controller and then send it to the view in this way:

DashboardController.php

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
   public function __contruct(){
     $this->middleware('auth');
   }

   public function index(){
     $data['usuario'] = User::find(1); //donde 1 es el ID que quieres consultar
     return view('dashboard', $data);
  }
}

And then you can print on your "dashboard" view in this way

{{ $usuario->name }}

I hope it serves you.

Greetings.

    
answered by 26.09.2018 в 20:56