SQL with associated data in Laravel?

2

I have the following tables listed below

Products

  • id
  • product
  • value
  • Validity

Queries

  • id
  • user_id

Product_Consults

  • id
  • query_id
  • product_id

Whenever a user performs a query, the history in the consultas table and its products are queried in the consultas_produtos table through the relationship.

How can I construct a SQL that when listing the products according to query parameters, return only the products that were never queried by the user ?

    
asked by anonymous 18.08.2018 / 14:42

1 answer

2

The join method can be used and at the end a # where to filter users, eg

$p = Produto::join('consultas_produtos','produtos.id','=','consultas_produtos.produto_id') 
               ->join('consultas','consultas_produtos.consulta_id','=','consultas.id') 
               ->where('consultas.user_id','<>', $user_id)
               ->get();

18.08.2018 / 15:04