Laravel query using SQL tags Like, Where, and, or

Asked

Viewed 391 times

0

Good afternoon guys I’m having a problem because I need to make the following representation of consultation in Aravel

SELECT * FROM imovels   AS i
INNER JOIN estados      AS e ON e.id = i.estado_id 
INNER JOIN cidades      AS c ON c.id = i.cidade_id
INNER JOIN bairros      AS b ON b.id = i.bairro_id
INNER JOIN tipos        AS t ON t.id = i.tipo_id
INNER JOIN finalidades          AS f ON f.id = i.finalidade_id


WHERE 
(i.finalidade_id = finalidadeId OR finalidadeId IS NULL) AND
(i.tipo_id = tipoId OR tipoId IS NULL) AND
(
i.titulo                        LIKE CONCAT('%',termo,'%') OR
i.descricao                     LIKE CONCAT('%',termo,'%') OR
i.valor                     LIKE CONCAT('%',termo,'%') OR
i.rua                   LIKE CONCAT('%',termo,'%') OR
i.codigo_interno                LIKE CONCAT('%',termo,'%') OR
e.nome                  LIKE CONCAT('%',termo,'%') OR
c.nome                  LIKE CONCAT('%',termo,'%') OR
b.nome                  LIKE CONCAT('%',termo,'%') OR
t.nome                  LIKE CONCAT('%',termo,'%') OR
f.nome                  LIKE CONCAT('%',termo,'%') );

but when I use

DB::table('imovels')->orWhere('tipos.nome', 'LIKE', '%' . $request->q . '%')

i can access by e.g. the name property of type but the array is mounted differently from when I do with the

Imovel::with('imagens', 'estado', 'cidade', 'bairro', 'user', 'tipo', 'finalidade')

the array is mounted correctly but I cannot compare the properties of the relation tables.

I do not know if I could be clear. But I appreciate any kind of help.

  • I will try to edit it so that it is more readable. Welcome David

  • blz smoothly

  • This may help you to understand the problem https://laravel.com/docs/5.7/queries#raw-Expressions

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.