php - 1055 Expression #3 of SELECT list is not in GROUP BY clause error in ManyToMany relationship laravel 5.3 -
i have post model this:
class post extends model { protected $primarykey = 'post_id'; public function tags () { return $this->belongstomany('app\tag'); } } and tag model:
class tag extends model { public function posts () { return $this->belongstomany('app\post'); } public function tagscount () { return $this->belongstomany('app\post') ->selectraw('count(pt_id) count') ->groupby('tag_id'); } public function gettagscountattribute() { if ( ! array_key_exists('tagscount', $this->relations)) $this->load('tagscount'); $related = $this->getrelation('tagscount')->first(); return ($related) ? $related->count : 0; } } (pt_id column primary key field in post_tag pivot table).
as see ,there manytomany relation between post , tag models.
for count related tags of specific post ,i added tagscount() , gettagscountattribute() methods tag model.
now suppose want tags count of specific post :
$post = post::find($post_id)->get(); return $post->tagscount it worked me in laravel 5.2 (and older versions) after upgrading laravel 5.3 , below error shown:
sqlstate[42000]: syntax error or access violation: 1055 expression #3 of select list not in group clause , contains nonaggregated column 'aids.post_tag.post_id' not functionally dependent on columns in group clause; incompatible sql_mode=only_full_group_by (sql: select count(pt_id) count, `post_tag`.`tag_id` `pivot_tag_id`, `post_tag`.`post_id` `pivot_post_id` `posts` inner join `post_tag` on `posts`.`post_id` = `post_tag`.`post_id` `post_tag`.`tag_id` in (145) , `posts`.`deleted_at` null group `post_tag`.`tag_id`) what problem , how can solve ?
this related mysql 5.7
long story short, 1 solution try changing config/database.php true false:
'mysql' => [ 'strict' => false, //behave 5.6 //'strict' => true //behave 5.7 ], for more info, see here: https://stackoverflow.com/a/39251942/2238694
Comments
Post a Comment