File: //home/xedaptot/be.naniguide.com/app/Model/Tutorial.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Library\Traits\HasUid;
class Tutorial extends Model
{
use HasFactory, HasUid;
protected $fillable = ['slug', 'category', 'sort_order', 'status'];
const STATUS_ACTIVE = 'active';
const STATUS_INACTIVE = 'inactive';
// -------------------------------------------------------------------------
public function translations()
{
return $this->hasMany(TutorialTranslation::class);
}
public function translation(string $locale)
{
return $this->translations()->where('locale', $locale)->first();
}
/**
* Get translated content, falling back to 'en' if requested locale is missing.
*/
public function getContent(string $locale = 'en'): ?TutorialTranslation
{
return $this->translation($locale)
?? $this->translation('en');
}
// -------------------------------------------------------------------------
// Scopes
public function scopeActive($query)
{
return $query->where('status', self::STATUS_ACTIVE);
}
public function scopeSearch($query, $keyword)
{
if (!empty($keyword)) {
$query->where(function ($q) use ($keyword) {
$q->where('slug', 'like', '%' . $keyword . '%')
->orWhere('category', 'like', '%' . $keyword . '%')
->orWhereHas('translations', fn ($t) =>
$t->where('title', 'like', '%' . $keyword . '%')
);
});
}
}
public function scopeSorted($query)
{
return $query->orderBy('sort_order')->orderBy('id');
}
// -------------------------------------------------------------------------
public static function findBySlug(string $slug): ?self
{
return self::where('slug', $slug)->first();
}
public function isActive(): bool
{
return $this->status === self::STATUS_ACTIVE;
}
}