It is not uncommon to store comma separated values in a mysql column in a Laravel Project
colours : "red,blue,green,yellow,black,white"
users : "77,4,5,688,5454,342,32,332"
tags : "mysql,laravel,css,html"
MySQL Function FIND_IN_SET() can be used to query
$search;
ModelName::whereRaw("FIND_IN_SET($search,colours)");
ModelName::whereRaw("FIND_IN_SET($search,users)");
ModelName::whereRaw("FIND_IN_SET($search,tags)");
$search;
ModelName::whereRaw("FIND_IN_SET(?,colours)",[$search]);
ModelName::whereRaw("FIND_IN_SET(?,users)",[$search]);
ModelName::whereRaw("FIND_IN_SET(?,tags)",[$search]);
class ModelName extends Model{
public function scopeContainsTag($query,$tag){
return $query->whereRaw("FIND_IN_SET(?,tags)",[$tag]);
}
}
You can call the scope while querying the model from a controller
public function index(Request $request){
ModelName::containsTag($request->tag_name)->get();
}
There are other ways of storing data in a column other than using comma separated, Read Laravel Documentation Array & JSON Casting