I have the following query using PDO, but how do I switch to Eloquent Laravel?

Asked

Viewed 71 times

-1

This serves for me to report through a timestamp in the column dating if my data has any modification.

inserir a descrição da imagem aqui

foreach ($lista_cnae as $cnpj => $lista) {
                    $lista = "'" . implode("','", $lista) . "'";
                    $stmt = $pdo->prepare("UPDATE cnaes_secundarios SET data_remocao = now() WHERE cnpj=:cnpj AND cnae NOT IN($lista) AND data_remocao IS NULL");
                    $stmt->execute([
                        'cnpj' => $cnpj,


                    ]);

                }
  • 1

    What models will you use and what SQL does?

  • I have a table where I do Insert and update and if the information has any changes like for example an update I inform to my removal column that that data was modified through a timestamp().

  • If you are using Eloquent, it would not be enough to create Models using soft delete?

  • What is your doubt? is to do the Model?

  • My doubt and how to create this query in Laravel.

2 answers

1

Use the Query Builder.

    \DB::table('cnaes_secundarios')
            ->where('cnpj', $cnpj)
            ->whereNotIn('cnae', $lista)
            ->whereNull('data_remocao')
            ->update('data_remocao', now()->toDateTimeString());

0


 foreach ($lista_cnae as $cnpj => $lista) {
        //  $lista = "'" . implode("','", $lista) . "'";
        Cnaes::where('cnpj', $cnpj)
            ->whereNull('data_remocao')
            ->whereNotIn('cnae',$lista)->update([
                'data_remocao'=>Carbon::now()
            ]);
    }

That was the solution to my doubt.

  • Then there was a Model...

Browser other questions tagged

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