How can i get n records from a model per parent in laravel eloquent.
For example lets say i have products
table and categories
table. And i want a list of all the products who's name starting with A
but not more then 10 products per category
.
My table structure is something like this.
products table
---------------------------------
id | title | slug | category_id
----------------------------------
Category table
--------------
id | title |
--------------
I tried to follow this example which is exactly what i want https://softonsofa.com/tweaking-eloquent-relations-how-to-get-n-related-models-per-parent/
But when i tried to query just like the example in the link after adding the scope in my product model
. It throwing and sql error saying.
SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation '=' (SQL: select count(*) as aggregate from...
Can anyone please tell how i can get n results per related model or how can i fix this error.