LARAVEL: Checking for SQL returns

Asked

Viewed 52 times

-1

I’m in need of some help!

I have an SQL query in Laravel as follows:

$cliente = DB::table('clientes')->where(DB::raw("cpf = '$cpf' AND (REPLACE(REPLACE(REPLACE(REPLACE(telefone1_num, '(', ''), ')',''), '-',''),' ','') LIKE '%$telefone%' OR REPLACE(REPLACE(REPLACE(REPLACE(telefone2_num, '(', ''), ')',''), '-',''),' ','') LIKE '%$telefone%' OR REPLACE(REPLACE(REPLACE(REPLACE(telefone3_num, '(', ''), ')',''), '-',''),' ','') LIKE '%$telefone%')"))->first();

I simply need to check if she’s returned any results. I’ve tried it this way:

if (is_null($cliente) || $cliente == null ) {
   return $this->sendError(config('global.cliente_nao_encontrado'));
}

But you still get into if. Does anyone know what it can be?

  • Make sure SQL returns results?

  • Yes friend! I tested it here already! In it I just remove the mask normally and compare!

  • Ever tried a dd($client); ?

  • the comparison is_null($cliente) and $cliente == null are the same thing; try to use the method exists(); $client->exists()

  • @RFL are not the same thing. A checks if it is null, the other if it is equal null. It looks the same, but how is used the loose comparison, the value false, for example, does not satisfy the first, but satisfies the second.

  • @Andersoncarloswoss is true, I had forgotten the ==, I ended up writing == thinking about ===

  • Guys, I was able to solve, the consultation in written Laravel for some reason includes a is null at the end of SQL. In short, there was no return, I did otherwise and with this check even worked!

Show 2 more comments

1 answer

-2

if($cliente->isNotEmpty()) {}

Or just like this

if($client){}
  • Could explain what this code does, where the colleague should put it, etc... It is not interesting to go out playing codes and not explain its function...

Browser other questions tagged

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