reconstruction of the project
This commit is contained in:
+77
-20
@@ -2,54 +2,111 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Section extends BaseModel
|
||||
{
|
||||
protected $table = 'sections';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
protected $fillable = [
|
||||
'section_name',
|
||||
'description',
|
||||
'updated_at',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
public $timestamps = false;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
|
||||
protected $casts = [
|
||||
'updated_by' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/* ============================================================
|
||||
* Relationships (optional)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public function updater(): BelongsTo
|
||||
{
|
||||
// Change User/Admin model to match your app
|
||||
return $this->belongsTo(Admin::class, 'updated_by');
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Scopes
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public function scopeNamed(Builder $q, string $name): Builder
|
||||
{
|
||||
return $q->where('section_name', $name);
|
||||
}
|
||||
|
||||
public function scopeSearch(Builder $q, string $term): Builder
|
||||
{
|
||||
$term = trim($term);
|
||||
if ($term === '') return $q;
|
||||
|
||||
return $q->where(function (Builder $w) use ($term) {
|
||||
$w->where('section_name', 'like', "%{$term}%")
|
||||
->orWhere('description', 'like', "%{$term}%");
|
||||
});
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* CI-compatible methods
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get section by name.
|
||||
*
|
||||
* @param string $sectionName
|
||||
* @return array|null
|
||||
* CI: getSectionByName()
|
||||
*/
|
||||
public function getSectionByName($sectionName)
|
||||
public static function getSectionByName(string $sectionName): ?self
|
||||
{
|
||||
return $this->where('section_name', $sectionName)->first();
|
||||
return static::query()->named($sectionName)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update section details by ID.
|
||||
*
|
||||
* @param int $sectionId
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* CI: updateSection() (Laravel automatically updates updated_at)
|
||||
*/
|
||||
public function updateSection($sectionId, $data)
|
||||
public static function updateSection(int $sectionId, array $data): bool
|
||||
{
|
||||
$data['updated_at'] = utc_now(); // Set the updated_at timestamp
|
||||
return $this->update($sectionId, $data);
|
||||
$section = static::query()->find($sectionId);
|
||||
if (!$section) return false;
|
||||
|
||||
// Laravel will set updated_at automatically when saving
|
||||
return $section->fill($data)->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete section by ID.
|
||||
*
|
||||
* @param int $sectionId
|
||||
* @return bool
|
||||
* CI: deleteSection()
|
||||
*/
|
||||
public function deleteSection($sectionId)
|
||||
public static function deleteSection(int $sectionId): bool
|
||||
{
|
||||
return $this->delete($sectionId);
|
||||
$section = static::query()->find($sectionId);
|
||||
if (!$section) return false;
|
||||
|
||||
return (bool) $section->delete();
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Optional validation helper (FormRequest/controller)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public static function rules(?int $id = null): array
|
||||
{
|
||||
return [
|
||||
'section_name' => ['required', 'string', 'max:255', 'unique:sections,section_name,' . ($id ?? 'NULL') . ',id'],
|
||||
'description' => ['nullable', 'string', 'max:2000'],
|
||||
'updated_by' => ['nullable', 'integer'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user