'boolean', 'urutan' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * Boot the model. */ protected static function boot() { parent::boot(); static::creating(function ($model) { if (Auth::check()) { $model->created_by = Auth::id(); $model->updated_by = Auth::id(); } }); static::updating(function ($model) { if (Auth::check()) { $model->updated_by = Auth::id(); } }); } /** * Scope untuk filter data aktif */ public function scopeActive($query) { return $query->where('is_active', true); } /** * Scope untuk filter berdasarkan kategori */ public function scopeByKategori($query, $kategori) { return $query->where('kategori', $kategori); } /** * Scope untuk urutkan berdasarkan urutan */ public function scopeOrdered($query) { return $query->orderBy('urutan', 'asc')->orderBy('name', 'asc'); } /** * Scope untuk pencarian */ public function scopeSearch($query, $search) { return $query->where(function ($q) use ($search) { $q->where('name', 'like', "%{$search}%") ->orWhere('link', 'like', "%{$search}%") ->orWhere('kategori', 'like', "%{$search}%") ->orWhere('deskripsi', 'like', "%{$search}%"); }); } /** * Relasi ke user yang membuat */ public function createdBy() { return $this->belongsTo(User::class, 'created_by'); } /** * Relasi ke user yang update */ public function updatedBy() { return $this->belongsTo(User::class, 'updated_by'); } /** * Accessor untuk status badge */ public function getStatusBadgeAttribute() { return $this->is_active ? 'Aktif' : 'Tidak Aktif'; } /** * Accessor untuk link yang diformat */ public function getFormattedLinkAttribute() { return $this->link ? url($this->link) : null; } /** * Mutator untuk memastikan link valid */ public function setLinkAttribute($value) { // Validasi dan format link if ($value && !preg_match('/^(https?:\/\/)/i', $value)) { $value = 'https://' . $value; } $this->attributes['link'] = $value; } }