Query in sql for Laravel

Asked

Viewed 171 times

0

This is the example of the consultation sql, I am trying to make this same consultation on Laravel if you can help.

SELECT p.nome,(select min(op.preco_unitario) from opcao_produto op where p.id = op.produto_id) preco from produto p WHERE (select min(op.preco_unitario) from opcao_produto op where p.id = op.produto_id) BETWEEN 202 and 1000

  • is doing with Eloquent?

1 answer

0

I tried to convert this same query using mainly the function raw.

of course this can be much more optimized!

The result was this (haven’t tested):

DB::select(
        DB::raw('p.nome'),
        DB::raw('(select min(op.preco_unitario) from opcao_produto op where p.id = op.produto_id)'),
        'preco'
    )->from(DB::raw('produto p'))
    ->whereRaw('(select min(op.preco_unitario) from opcao_produto op where p.id = op.produto_id)')
    ->whereBetween('COLUNA', [202, 1000])->get();
  • Well tested query is gave this error Errorexception (E_ERROR) Type error: Argument 1 passed to Illuminate Database Connection::prepareBindings() must be of the type array, Object Given.

Browser other questions tagged

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