Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query that worked in Laravel 5.2 give me error in Laravel 5.3

This query works in 5.2:

    $galleries = Gallery::with(array(
            'images' => function ($query) {
                $query->orderBy('order', 'asc');
            }
        ))
        ->with('votes')
        ->leftJoin('votes', 'votes.votable_id', '=', 'gallery.id')
        ->selectRaw(
            'gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status when "downvote" then 1 else null end) as points'
        )
        ->where('votes.votable_type','App\Gallery')
        ->groupBy('gallery.id')
        ->orderBy('points', 'desc')
        ->published()->orderBy('gallery.created_at', 'desc')->paginate(30);

I am trying to select all galleries that have votes, when I run this in 5.3 I get this

 1/2 PDOException in Connection.php line 333: SQLSTATE[42000]: 
Syntax error or access violation: 1055 'images.gallery.title' isn't in GROUP BY 

SQLSTATE[42000]: Syntax error or access violation: 1055 'images.gallery.title' isn't in 
GROUP BY (SQL: select gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status when "downvote" then 1 else null end) 
as points from `gallery` left join `votes` on `votes`.`votable_id` = `gallery`.`id`
where `votes`.`votable_type` = App\Gallery and `published` = 1 group by `gallery`.`id` 
order by `points` desc, `gallery`.`created_at` desc limit 30 offset 0)
like image 588
niko craft Avatar asked Aug 24 '16 10:08

niko craft


2 Answers

Go to your config/database.php folder. In mysql configuration array, change strict => true to strict => false, and everything will work nicely.

like image 182
Swapnil Bhikule Avatar answered Nov 18 '22 07:11

Swapnil Bhikule


As per documentation your join query should like this Reference -:https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0

 $galleries = Gallery::with(array(
        'images' => function ($query) {
            $query->orderBy('order', 'asc');
        }
    ))
    ->with('votes')
    ->leftJoin('votes',function($query){
                 $query->on('votes','gallery')->where('votes', 'votes.votable_id', '=', 'gallery.id')->where('votes.votable_type','App\Gallery');   })->selectRaw(
        'gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status when "downvote" then 1 else null end) as points'
    )
    ->groupBy('gallery.id')
    ->orderBy('points', 'desc')
    ->published()->orderBy('gallery.created_at', 'desc')->paginate(30);
like image 45
Hamza Dairywala Avatar answered Nov 18 '22 07:11

Hamza Dairywala