@zefex It sounds like you have three models, then: an Ad, a tag type, and tags.
Different types of tags/categories for a model
I have an Ad model and I wanted to add services and payment methods that this ad accepts, for example, let's say that this Ad made by a man that works with electricity, painting, general services, and also he accepts credit card, bank transfer payments.
What would be the best way to handle multiple tagging/categories system? Is there a library to help with that or I just have to create a many to many model for each relationship like this? Is there a way to make it more reusable so I don't have to be writing many to many relationships to every single different tagging option for the user? For example: services, extra services, payment methods accepted.
The reason I want it as tags/categories is because I will make a search with filters so the users can find Ads based on what they want, like I want someone that works with electricity and also accepts bank transfer.
Dunno that it's really necessary, but yeah there is laravel-categories .. and a few others, but this appears to be the most popular, and was last updated today.
I think you could probably get by with your tags/categories having a 'type' flag, assuming you'll have a (even if large) limited number of types, in your example you have services and payment types, so lets go with that...
Your tags could either belongTo a parent Type as @martinbean is suggesting, or you could just as easily use an enum or a string value, especially if the number of types will be limited.
Then for ease you'd probably want your ads to have multiple relationships for the various types, ie:
class Ad extends Model {
public function services() {
return $this->belongsToMany(Tag::class)
->where('type', 'service')
}
public function paymentMethods() {
return $this->belongsToMany(Tag::class)
->where('type', 'payment_method')
}
}
Or, it may make sense to just go ahead and keep payment_methods and services totally separated as their own models. That's probably the route I'd go just based on the limited details you've provided as it's likely that payment_methods will have other stuff associated with them, and likewise, perhaps, will services. Those strike me more as different types of entity than simple tags.
Please or to participate in this conversation.