Query com Laravel

Asked

Viewed 53 times

-1

Hello , as would be in lavarel a select like this example:

SELECT DADO FROM TABELA WHERE ID = '123' AND (FLAG IS NULL OR FLAG = 0)  

I couldn’t find similar examples out there :(

  • You can see more about getting data from the database with Laravel in the https://laravel.com/docs/6.x/queries#retrieving-Results (using the DB class) or https://laravel.com/docs/6.x/eloquent#retrieving-models (using Eloquent ORM)

1 answer

1

I believe that one of the examples below can help you:

# Utilizando a classe DB 
$users = DB::table('users')
           ->where('id', '=', 123)
           ->where(function ($query) {
               $query->whereNull('flag')
                     ->orWhere('flag', '=', 0);
           })
           ->select('name')
           ->get();

# Utilizando o Model User
$users = User::where('id', '=', 123)
             ->where(function ($query) {
                 $query->whereNull('flag')
                     ->orWhere('flag', '=', 0);
             })
             ->select('name')
             ->get();
  • You can start the query with Users::query().

  • 1

    It’s an option too, I’ll edit and put the two options.

Browser other questions tagged

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