I can not get the user list displayed

1

You see, I have a table User:

Schema::create('users', function(Blueprint $table){
            $table->increments('id');
            $table->string('name');
            $table->string('second_name')->nullable();
            $table->string('provincia')->nullable();
            $table->string('localidad')->nullable();
            $table->string('direccion')->nullable();
            $table->string('telefono');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('dni');
            $table->boolean('vehiculo')->default(false);
            $table->string('foto')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });

I want to make a view that allows to see all the users. For this I have this link in web.php:

Route::get('/inicio', 'UserController@index');

I have this code in UserController.php:

public function index(){
        $usuarios=User::latest()->paginate(5);
        return view('menus.inicio',compact($usuarios));
    }

And I have the following view:

@extends('layouts.app')
@section('content')
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h1 class="text-center text-mute"> Usuarios </h1>
            @forelse($usuarios as $u)
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h2> {{ $u->name }} </h2>
                    </div>
                    <div class="panel-body">
                        <h4>{{ $u->dni }}</h4>
                        <h4>{{ $u->telefono }}</h4>
                    </div>
                </div>
            @empty
                <div class="alert alert-danger">
                    <h1>No hay ningún usuario por el momento</h1>
                </div>
            @endforelse
        </div>
    </div>
@endsection

But I get the following error:

If I use dd ($ users) it works, so I would like to know what I do wrong in the view.

    
asked by Miguel Alparez 12.04.2018 в 18:06
source

2 answers

2

If you use the compact method, you do not need the dollar sign

public function index(){
        $usuarios=User::latest()->paginate(5);
        return view('menus.inicio',compact('usuarios'));
    }

Or it can be with the with method, so

public function index(){
        $usuarios=User::latest()->paginate(5);
        return view('menus.inicio')->with('usuarios', $usuarios);
    }
  

Both shipping methods are valid, the only difference will be that with   compact the name of the variable is sent as if it were string   text and with the with first you declare a kind of alias with the same   name of the variable and separated by a comma now if the name of the   variable that contains your data

    
answered by 12.04.2018 / 18:21
source
0

The error is in the way you send the variable

  

$ users

in plain sight, just remove the $ symbol and enclose it in quotes:

 public function index(){
        $usuarios=User::latest()->paginate(5);
//Quitarle el simbolo de dolar y encerrar entre comillas variable enviada en compact
        return view('menus.inicio',compact('usuarios'));
    }
    
answered by 12.04.2018 в 18:08