1055 Expression #3 of SELECT list is not in GROUP BY clause error in ManyToMany relationship laravel 5.3
<p>I have a <code>Post</code> Model like this:</p>

<pre><code>class Post extends Model
{
        protected $primaryKey = 'post_id';
        public function tags ()
        {
            return $this-&gt;belongsToMany('App\Tag');
        }

}
</code></pre>

<p>and a <code>Tag</code> Model:</p>

<pre><code>class Tag extends Model
{
        public function posts ()
        {
            return $this-&gt;belongsToMany('App\Post');
        }

        public function tagsCount ()
        {
            return $this-&gt;belongsToMany('App\Post')
                -&gt;selectRaw('count(pt_id) as count')
                -&gt;groupBy('tag_id');
        }

        public function getTagsCountAttribute()
        {
            if ( ! array_key_exists('tagsCount', $this-&gt;relations)) $this-&gt;load('tagsCount');

            $related = $this-&gt;getRelation('tagsCount')-&gt;first();

            return ($related) ? $related-&gt;count : 0;
        }
}
</code></pre>

<p>(<code>pt_id</code> column is primary key field in <code>post_tag</code> pivot table).</p>

<p>As you see ,there is a <strong>ManyToMany</strong> relation between <code>Post</code> and <code>Tag</code> Models.</p>

<p>For count related Tags of a specific Post ,I added <code>tagsCount()</code> and <code>getTagsCountAttribute()</code> methods to <code>Tag</code> Model.</p>

<p>Now suppose I want to get tags Count of a specific Post like this :</p>

<pre><code>$post = Post::find($post_id)-&gt;get();
return $post-&gt;tagsCount
</code></pre>

<p>It worked for me in laravel 5.2 (and older versions) but after upgrading to laravel 5.3 , below error is shown:</p>

<pre><code>SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'aids.post_tag.post_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select count(pt_id) as count, `post_tag`.`tag_id` as `pivot_tag_id`, `post_tag`.`post_id` as `pivot_post_id` from `posts` inner join `post_tag` on `posts`.`post_id` = `post_tag`.`post_id` where `post_tag`.`tag_id` in (145) and `posts`.`deleted_at` is null group by `post_tag`.`tag_id`)
</code></pre>

<p>What is Problem and how can I solve that ?</p>