eloquent-power-joins icon indicating copy to clipboard operation
eloquent-power-joins copied to clipboard

PoweJoinWhereHas on Pivot table is not working

Open navaneeth-raman-bhaskar opened this issue 3 years ago • 1 comments
trafficstars

Steps to reproduce

Laravel 8.0 MySql 5.7 PHP 7.4

class Product extends Model
{
 public function productCategories()
    {
        return $this->hasMany(ProductCategory::class, 'product_id', 'id');
    }
}

public function scopeType($q, $type)
    {
        return $q->where('type',$type);
    }
}
class ProductCategory extends Pivot
{
}
class Category extends Model
{
}

Controller code

 $products = Product::type(request->type)->powerJoinWhereHas('productCategories', function ($query) use ($request) {
                $query->whereIn('category_id', $request->categoryIds);  /* here [1,2] */
 })->paginate();

SQLSTATE[42000]: Syntax error or access violation: 1055 'products.type' isn't in GROUP BY (SQL: select count(*) as aggregate from (select products.*, count(product_categories.id) as product_categories_count from products left join product_categories on product_categories.product_id = products.id and category_id in (1, 2) and products.deleted_at is null and type = 1 group by products.id having product_categories_count >= 1) as aggregate_table)

navaneeth-raman-bhaskar avatar Mar 07 '22 05:03 navaneeth-raman-bhaskar

If anyone having same issue on pivot table; Use native whereHas() instead

navaneeth-raman-bhaskar avatar Mar 07 '22 10:03 navaneeth-raman-bhaskar

Can you verify if this is still an issue with 3.0.0? If so please reopen the issue

luisdalmolin avatar Mar 05 '23 12:03 luisdalmolin