eloquent-power-joins
eloquent-power-joins copied to clipboard
PoweJoinWhereHas on Pivot table is not working
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 fromproductsleft joinproduct_categoriesonproduct_categories.product_id=products.idandcategory_idin (1, 2) andproducts.deleted_atis null andtype= 1 group byproducts.idhaving product_categories_count >= 1) asaggregate_table)
If anyone having same issue on pivot table; Use native whereHas() instead
Can you verify if this is still an issue with 3.0.0? If so please reopen the issue