Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

rmcquary rmcquary Joined 20 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.