+1 vote
Good night all. Faced with the difficulty alone. A model Guides , which has fields id, categories_id . And categories_id is stored in json format ["1","2","3"]. And the bottom line is this: I need to find all rows from database where fields in the json is valid categories_id figure 1.

1 Answer

0 votes
Best answer
How ...?
Stop velosipedit and make normal tables and relationships. HasMany| Many to Many here is perfect (don't know how it should work there).


Table guides:
id | name
Table categories:
id | name
Table for communication (category_guide):
guide_id | category_id

Prescribed for the model Guide:
public function categories()
return $this->belongsToMany('App\Models\Category');
And for the model Category:
public function guides()
return $this->belongsToMany('App\Models\Guide');
Now you can just:
$ids = [1, 2, 3];
$guides = App\Models\Guide::whereHas('categories', function ($q) use ($ids)
return $q->whereIn('id', $ids);
Get a list of all guides that entering the category with ID = 1, 2, 3 Or to get a number of guides in categorie:
$category = Category::find(1);
$count = $category->guides()->count();