Error using whereRaw in Laravel

Asked

Viewed 1,415 times

2

What am I doing wrong in this queryRaw? The goal is to display the summed values if ID = ID, but I’m getting the total sum of the column.

Excerpt from *query made on Controller:

//Show
public function show( Pesquisa $pesquisa )
{
    $this->guardaAvaliacao( $pesquisa );

    $pesquisa->load( [
        'respostas' => function ( $relation ) {
            $relation->with( [ 'pergunta.opcoes', 'opcao' ] );
        }
    ] );

    $nota = DB::table('clima.tbl_resposta_super')
        ->selectRaw('sum(nota_original) as nota_original ')
        ->whereRaw('tbl_resposta_super.pesquisa_id', '=' .$pesquisa->id)
        ->groupBy('nota_original')
        ->get();


    dd($nota);

}
  • Test by placing a single quotation mark on the url or input if it is the case of the search). Use, say, 100% in the Laravel way. Use thus ->whereRaw('tbl_resposta_super.pesquisa_id = ?' , [$search->id])

2 answers

2


When using the whereRaw need not separate the operator as an argument, as if it were the where.

$nota = DB::table('clima.tbl_resposta_super')
        ->selectRaw('sum(nota_original) as nota_original ')
        ->whereRaw('tbl_resposta_super.pesquisa_id = ' .$pesquisa->id)
        ->groupBy('nota_original')
        ->get();
  • Good morning! Thanks for the tip, I solved the problem as follows, //Recovers and sums up Notes $notes = DB::table('clima.tbl_resposta_super') ->select(DB::raw('sum(nota_original) as nota_original,sum(nota_supervisor) as nota_supervisor, pesquisa_id') ->Where('tbl_resposta_super.pesquisa_id', '=', $search->id) ->groupBy('pesquisa_id') ->get();

0

For the next ones who have the same doubt follows resolution.

        //Recupera e soma Notas
    $notas = DB::table('clima.tbl_resposta_super')
        ->select(DB::raw('sum(nota_original) as nota_original,sum(nota_supervisor) as nota_supervisor, pesquisa_id'))
        ->where('tbl_resposta_super.pesquisa_id', '=', $pesquisa->id)
        ->groupBy('pesquisa_id')
        ->get();

Browser other questions tagged

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