sementara
This commit is contained in:
152
app/Models/ReferensiLink.php
Normal file
152
app/Models/ReferensiLink.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Modules\Usermanagement\Models\User;
|
||||
|
||||
class ReferensiLink extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'referensi_link';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'link',
|
||||
'kategori',
|
||||
'deskripsi',
|
||||
'is_active',
|
||||
'urutan',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'is_active' => '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
|
||||
? '<span class="badge bg-success">Aktif</span>'
|
||||
: '<span class="badge bg-danger">Tidak Aktif</span>';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user