see table with the creations of the logged in user

0

I have this table of products and I want to show the values of only that one by the user that is logged.

my controller

public function mostrarTablausu()
    {          

        $productos = productos::with('usuario','categoria','estado','mensajeson')->select('productos.*');


        return  Datatables::of($productos)->addColumn('action', function($productos)
        {
            return 
            '<a type="button" class="btn btn-info" href="">Aprovar</a>'.
            '<a type="button" class="btn btn-danger" href="">Rechazar</a>'.
            '<a onclick=""  class="btn btn-warning" >Verificar</a>';

        })->make(true);
    }

I do not know how to show me only the products that only the logged-in user believes.

    
asked by Jhon Bernal 09.07.2018 в 22:40
source

2 answers

0

You use the Auth facade to get the id of the logged-in user and where you relate it to the primary key of your user table.

public function mostrarTablausu()
{          

    $productos = productos::with('usuario','categoria','estado','mensajeson')->select('productos.*')->where('usuario.id','=',Auth::user()->id);


    return  Datatables::of($productos)->addColumn('action', function($productos)
    {
        return 
        '<a type="button" class="btn btn-info" href="">Aprovar</a>'.
        '<a type="button" class="btn btn-danger" href="">Rechazar</a>'.
        '<a onclick=""  class="btn btn-warning" >Verificar</a>';

    })->make(true);
}
    
answered by 10.07.2018 / 08:41
source
0

The helper auth (or the facade Auth ) helps you bring User information logged:

// para obtener al usuario:
$usuario = auth()->user();

// o solo para traer el id del usuario:
$id_usuario = auth()->id();

So, the pending thing that you have left to do is to adjust your query so that it brings the elements that are related only to that user. A simple example:

// obtenemos los productos vinculados con el usuario logueado
// pd: aquí asumo que existe la relación "productos" en el modelo Usuario
$productos = App\Usuario::find(auth()->id())->productos;
// con lo siguiente vamos a cargar el resto de relaciones:
$productos->load('usuario','categoria','estado','mensajeson');

Documentation:

answered by 10.07.2018 в 08:13