I'm no expert, so I don't know if this would work or not... but could you create a BaseModel class that has the $slugs = Model::where('slug', 'LIKE', $slug . "%")->orderBy('slug', 'desc')->first(); code in it... and have all your models extend that BaseModel class, and should therefore have access to that method. Just a thought.
Josh, I like this idea, but I should have mentioned that I am using Eloquent. So, I guess that my BaseModel class should extend Eloquent, and then have the Model extend BaseModel?
Instead of "Model::where" you can use construction _ _ CLASS _ _::where - this way query initiated by construction will use appropriate table. But as far as you want to write down slug to the model's table it is better to move slug's generating to trait. Because BaseModel by its meaning should be parent class for ALL the models in the app. You can move slug-related logic to some SlugModel class that will be parent for part of your models' classes.
Or you can just save all slug in one table :) This way not slug itself but pair "slug + model's class name" should be unique. Also this way you definitely can save it right into BaseModel.
PS it is pretty strange for PHP-related forum to keep two underscores as metatag for bold text.
skyboyer said:
Instead of "Model::where" you can use construction _ _ CLASS _ _::where - this way query initiated by construction will use appropriate table.
Thanks skyboyer, this is what I was looking for, but appreciate your other suggestions. Building a trait sounds appealing as well.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community