If you can use the whereRaw to write a Sub Query as follows:
$subQuery = 'relacionado = (SELECT relacionado FROM produto WHERE id=? limit 1)';
Produto::whereRaw($subQuery, array(7996))->get();
would then be a SQL but having a SubQuery for the search of the field relacionado.
The way you did it is not wrong only is it wrong to bring all fields that are not required for the second search, an optimization:
$result = Produto::where('id', 7996)->select('relacionado')->first(); // otimizado
$relacionados = Produto::where('relacionado', $result->relacionado)->get();
A summary of the code can be done as explained in this question, with Query Scope as follows:
Create a method in your class Produto:
public function scopeRelacionado($query, $id)
{
$subQuery = 'relacionado = (SELECT relacionado FROM produto WHERE id=? limit 1)';
return $query::whereRaw($subQuery, array($id))->get();
}
and use as follows:
Produto::relacionado(7996);
Can make a subQuery for the search of
produto->relacionado? that would be it?– novic
That. How could I pass the value of the "related" field of the product found in find to that subquery?
– Diego Vieira