With putting together a query on Eloquent?

Asked

Viewed 89 times

-4

How do I put this query in Eloquent?

SELECT juros FROM tbl_parcelas WHERE 2500 BETWEEN tbl_parcelas.valor_min AND tbl_parcelas.valor_max

CODE:

DB::table('SELECT juros FROM tbl_parcelas WHERE 2500 BETWEEN tbl_parcelas.valor_min AND tbl_parcelas.valor_max');
  • Hello André, what have you tried so far? You are having difficulty in some specific part of the query?

  • I don’t know exactly how to use Eloquent syntax for this query

  • Solution: DB::select("SELECT INTEREST FROM parcels WHERE $value BETWEEN valor_min AND valor_max");

3 answers

3

You can do this in two ways, the first would be using the Model of this table and the second would be using the facade DB In the case of this second option, the.

// Exemplo utilizando model
$valor = 2500;
$juros = Parcela::where('valor_min' '>=', $valor)
                ->where('valor_max', '<=', $valor)
                ->select('juros')
                ->get();

// Exemplo usando DB
$juros = DB::table('tbl_parcelas')->where('valor_min' '>=', $valor)
                                  ->where('valor_max', '<=', $valor)
                                  ->select('juros')
                                  ->get();
  • Kayo Bruno, thanks for the tip but this way returns null,

  • Solution: DB::select("SELECT INTEREST FROM parcels WHERE 2500 BETWEEN valor_min AND valor_max");

  • If returns null is pq has no record q please query.

  • True, thank you very much helped me solve it. Grateful.

0

By the time you are the post should have solved, but I would do so:

 $juros = DB::table('parcelas')
                 ->select('juros')
                 ->whereRaw("? between valor_min and valor_max", [$valor])
                 ->get();

Or else:

 $juros = DB::table('parcelas')
                 ->select('juros')
                 ->whereColumn([
                     ['valor_min', '>=', $valor],
                     ['valor_max', '<=', $valor]
                  ])->get();

0


Solution for this case:

$valor = 2500;
DB::select("SELECT juros FROM parcelas WHERE $valor BETWEEN valor_min AND valor_max");

Browser other questions tagged

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