Filter results by logged-in user id

0

I am building a system using the Laravel framework (I have little knowledge in fw) and I need to get the id of the user who logged in to do the queries based on the userid.

Ex: UserUm = > lists all products registered with the user id.

How can I do this?

    
asked by anonymous 01.10.2014 / 21:41

3 answers

1

You can also use the HasMany relationship in the user model, considering that you have in the table the user key (userid)

class Usuario extends Eloquent{
  public function produtos(){
    return $this->HasMany('Produto');
  } 
}

class Produto extends Eloquent{
    public function usuario(){
        return $this->belongsTo('Usuario')
    }
}

and in the query you can do:

if(Auth::check()){ //se tem usuario logado
    $usuario_produtos = Auth()->user()->produtos();
}
    
01.10.2014 / 21:57
0

You can use Auth to get the user who logged into the system:

if (Auth::check()){ //verifica se tem usuario logado
    $usuario_id = Auth::user()->id;
    $usuario_produtos  =  Produto::where('usuario_id', $usuario_id);
}
    
01.10.2014 / 21:52
0
$userID = Auth::user()->id;

or

$id = Auth::id();

link

Example:

$products = Product::where('user_id', Auth::id())->get();

Or if you have the relationship between user and products, simply:

$products = Auth::user()->products();
    
01.10.2014 / 21:48