reconstruction of the project
This commit is contained in:
+34
-44
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
@@ -8,9 +7,10 @@ use App\Models\BaseModel;
|
||||
class ContactUs extends BaseModel
|
||||
{
|
||||
protected $table = 'contactus';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $useSoftDeletes = false;
|
||||
|
||||
// ✅ CI: useTimestamps = true (created_at/updated_at)
|
||||
public $timestamps = true;
|
||||
|
||||
protected $fillable = [
|
||||
'sender_id',
|
||||
'reciever_id',
|
||||
@@ -22,71 +22,61 @@ class ContactUs extends BaseModel
|
||||
'school_year',
|
||||
];
|
||||
|
||||
// Timestamps (set manually or use auto timestamps below)
|
||||
public $timestamps = true;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
|
||||
protected $validationRules = [
|
||||
'sender_id' => 'required|integer',
|
||||
'reciever_id' => 'required|integer',
|
||||
'subject' => 'required|string|max_length[255]',
|
||||
'message' => 'required|string',
|
||||
'semester' => 'required|string|max_length[255]',
|
||||
'school_year' => 'permit_empty|string|max_length[9]'
|
||||
protected $casts = [
|
||||
'sender_id' => 'integer',
|
||||
'reciever_id' => 'integer',
|
||||
];
|
||||
|
||||
protected $validationMessages = [];
|
||||
protected $skipValidation = false;
|
||||
/* Optional relationships */
|
||||
public function sender()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'sender_id');
|
||||
}
|
||||
|
||||
public function receiver()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'reciever_id');
|
||||
}
|
||||
|
||||
/* =========================
|
||||
* CI method equivalents
|
||||
* ========================= */
|
||||
|
||||
/**
|
||||
* Retrieve messages by semester and school year.
|
||||
*
|
||||
* @param string $semester
|
||||
* @param string $school_year
|
||||
* @return array
|
||||
*/
|
||||
public function getMessagesBySemesterAndYear($semester, $school_year)
|
||||
public static function getMessagesBySemesterAndYear(string $semester, string $schoolYear)
|
||||
{
|
||||
return $this->where([
|
||||
'semester' => $semester,
|
||||
'school_year' => $school_year
|
||||
])->findAll();
|
||||
return static::query()
|
||||
->where('semester', $semester)
|
||||
->where('school_year', $schoolYear)
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve messages for a specific sender or receiver.
|
||||
*
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
public function getMessagesForUser($userId)
|
||||
public static function getMessagesForUser(int $userId)
|
||||
{
|
||||
return $this->where('sender_id', $userId)
|
||||
return static::query()
|
||||
->where('sender_id', $userId)
|
||||
->orWhere('reciever_id', $userId)
|
||||
->findAll();
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a message by ID.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array|null
|
||||
*/
|
||||
public function getMessageById($id)
|
||||
public static function getMessageById(int $id): ?self
|
||||
{
|
||||
return $this->find($id);
|
||||
return static::query()->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a message by ID.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function updateMessage($id, $data)
|
||||
public static function updateMessage(int $id, array $data): bool
|
||||
{
|
||||
return $this->update($id, $data);
|
||||
return static::query()->whereKey($id)->update($data) >= 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user