Ad

Laravel 5.2 Shows "The Response Content Must Be A String Or Object Implementing __toString(), "boolean" Given" When I Try To Use DB::statement

- 1 answer

I'm trying to use DB::statement() method to run a SQL, but I can't get it to work and it shows "UnexpectedValueException in Response.php line 395: The Response content must be a string or object implementing __toString(), "boolean" given."

Here's the code:

$students = DB::statement('select stu_agency,avg(stu_rank1) as avggrade from students group by stu_agency');
return $students;
Ad

Answer

From docs:

Some database statements should not return any value. For these types of operations, you may use the statement method on the DB facade.

You are searching for

$students = DB::table('students')
                  ->select('stu_agency', DB::raw('avg(stu_rank1) as avggrade'))
                  ->groupBy('stu_agency')
                  ->get();
return $students;
Ad
source: stackoverflow.com
Ad