I have a function in which I take model as parameter to create slug value from that model values and after creating slug i want to check existence of that in same table.
public static function slug_it($model = null)
{
if(!empty($model)) {
#from model
$keys = [/*priority => key to find*/
1 => 'name', 2 => 'item', 4 => 'title', 3 => 'code', 5 => 'company', 6 => 'code_name', 7 => 'country', 8 => 'city', 9 => 'state', 10 => 'username', 11 => 'first_name', 12 => 'last_name',];
/* suppose i have slug */
$slug = 'item_erp5511';
/*slug or sku*/
} else {
}
}
Suppose Model is of item table and I want to find slug in "slug" col of that table but i only have Model Object of that table.
You can try something like the following, presumption, $model = Item::class
instance and you want to find something from the slugs
table.
// $model = Item::find(1);
$model->getQuery()->from('slugs')->where('slug', $slug)->get();
Alternatively if slug_it
is inside a model you can try:
self::getQuery()->from('slugs')->....->get();
Update after your latest comment
Inside the method to do a check you can do:
if (self::where('slug', $slug)->exists()) {
// Do what you need
}