HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.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;
    }
}