2
Hello,
I’m trying to create an advanced query, but I’m having some problems following the code for analysis!
Anuncio::join('users', function ($users) use ($request){
$users->on('users.id' , '=', 'a.user');
})->join('enderecos', function ($enderecos) use ($request){
$enderecos->on('enderecos.id', '=', 'users.endereco');
})->where('enderecos.uf_id', '=', $request['uf_id'])->get();
The above query works perfectly, but when I try to adapt it to another group of filters it does not work and error!
Follow the other filter group:
$anuncios = Anuncio::where(function($query) use ($request)
{
if(isset($request['uf_id'])){
$query->join('users', function ($users) use ($request){
$users->on('users.id' , '=', 'a.user');
})->join('enderecos', function ($enderecos) use ($request){
$enderecos->on('enderecos.id', '=', 'users.endereco');
})->where('enderecos.uf_id', '=', $request['uf_id']);
}
if(isset($request['modelo'])){
$query->where('modelo', $request['modelo']);
}
if(isset($request['marca']))
$query->where('marca', $request['marca']);
})->with('anuncio_dados')->with('adicionais')
->with('anuncio_imagens')->with('versaos')
->with('marcas')->with('modelos')->paginate(10);
When I try to insert it into this query, the uf_id filter does not work. Can anyone tell me why?
First thing the
joins
can be simplified already since you don’t use $request or $users ( and others) inside can only be the passage of values. Another point:quando tento inserir ele nesta query o filtro de uf_id não funciona. alguem sabe me dizer o por que?
because, error appears what?– novic
the error that gives this message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'addressees.uf_id' in 'Where clause' (SQL: select Count(*) as Aggregate from
anuncios
asa
Where (enderecos
.uf_id
= 15))"– Lodi
There is the column
uf_id
within addresses?– novic
yes, and as I mentioned in the post what left me not knowing what to do, is that in the first query the return is valid!
– Lodi
I found the problem, you can’t do Where with
join
... you need to do everything separately, always using the latest version of the collected Builder!– novic
Gratitude for your help, but how would that be? I was doubtful how to do!
– Lodi