Obtain data from a user in Laravel

2

They ask me to show the data of a user (using their id), I have created the table and the model with artisan, I have filled two objects in the phpmyadmin database, in the model I have Put this:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Firma extends Model
{
protected $table = 'empleados'; // Nombre de la tabla
protected $primaryKey = 'id_empleado'; // Clave primaria

// Columnas de la tabla
protected $fillable = ['id_perfil','nombre_empleado','puesto','telefono_empleado','email_empleado','direccion_empleado','status_empleado','date_added'];
}

On the controller this:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
// Incluimos el modelo
use App\Firma;

class FirmaController extends Controller
{
// Creamos un método para atender la peticion llamado show
public function Index($id)
{
    // Buscamos el id en la tabla
    $dato = Auth::user()->id;
    // retornamos la vista con los datos 
    return view('firmas')->with('dato', $dato);
    // with() nos permite pasar variables a la vista
    // el primer parámetros es el nombre con el que estará disponible en la vista
    // el segundo son los datos. 
}
}

I defined the route:

Route::get('/firmas', '[email protected]')->name('firmas');

But when I want to show the data that the table has in the database like this:

{{ $dato->nombre }}

I get the error:

Too few arguments to function App\Http\Controllers\FirmaController::Index(), 0 passed and exactly 1 expected"

I do not know what I'm doing wrong, I just want to show data that I have in a table and follow this tutorial

    
asked by Abdiel Hernandez 08.07.2018 в 22:16
source

2 answers

0

The problem is in the controller, you are not making a query for that I let add the following code

$dato = Auth::user()->id;
$dato=Firma::where('id_perfil',$dato)->get();
var_dump($dato);

just bring me the field id, do not bring me the registration id

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
// Incluimos el modelo
use App\Firma;

class FirmaController extends Controller
{
// Creamos un método para atender la peticion llamado show
public function Index($id)
{
    // Buscamos el id en la tabla
    $dato = Auth::user()->id;
    $dato=Firma::where('id',$dato)->get();
    var_dump($dato);//me imprimer la consulta 
    exit();//en caso que revisar los datos 
    // retornamos la vista con los datos 
    return view('firmas')->with('dato', $dato);
    // with() nos permite pasar variables a la vista
    // el primer parámetros es el nombre con el que estará disponible en la vista
    // el segundo son los datos. 
}
}
    
answered by 08.07.2018 в 22:57
0

There are a couple of wrong things in your code.

Obs 1: Declaration and call of the method index

Notice how the method is declared:

public function Index($id) { /* ... */ } // ojo también con la mayúscula.

This method expects to receive an argument ( $id ). On the other hand, this is the way it is called from your routes:

Route::get('/firmas', '[email protected]')->name('firmas');

As there is no route parameter no argument is being passed to the method, this is what causes the error received:

  

Too few arguments to function App \ Http \ Controllers \ FirmaController :: Index (), 0 passed and exactly 1 expected "

This error tells you that the method expects an argument but received less.

To solve it, it would be enough to add the parameter in the aforementioned route, like this:

Route::get('/firmas/{id}', '[email protected]')->name('firmas');

In this way, the value of id is sent as the first argument in the index method.

Obs 2: The index method

I notice that the variable $id is not really used in your function. According to the comments you mention in your function, this method is one of type show , which usually show a particular element. Now, there are two options that you might be interested in:

( Option A ) Return the user's signature with id = $id

( Option B ) Return the user's signature with active session (logged in)

Option A

To do this, do the following:

public function index($id)
{
    // Buscamos el id en la tabla
    $usuario = App\Firma::find($id);
    // retornamos la vista con los datos 
    return view('firmas')->with('dato', $usuario);
}

Option B

For option B we do the following:

public function index() // como no se usará el id, eliminamos el argumento
{
    // Buscamos el id en la tabla
    $usuario = App\Firma::find(auth()->id());
    // retornamos la vista con los datos
    return view('firmas')->with('dato', $usuario);
}

Observation

In your code you did the following:

$dato = Auth::user()->id;

This brings you the id of the logged in user, but not the whole object, so the following code in your view should throw you error since $dato is not an object but a integer :

{{ $dato->nombre }}

If you want to return the complete object you can do this:

$dato = App\Firma::find(Auth::user()->id);
    
answered by 08.07.2018 в 23:08