-
Notifications
You must be signed in to change notification settings - Fork 0
Models
Tirapong Chaiyakun edited this page Sep 19, 2026
·
1 revision
php deawx make:model Usernamespace App\Models;
use Core\Model;
final class User extends Model
{
protected static string $table = 'users';
protected static array $fillable = ['name', 'email', 'password'];
protected static bool $timestamps = true;
}ถ้า $fillable ว่าง ระบบจะไม่รับ mass assign ใด ๆ
ฟิลด์ใน $guarded ถูกกันเสมอ
User::create(['name' => 'Deawx', 'email' => 'a@example.com']);
$user = User::find(1);
$user = User::findOrFail(1); // ไม่เจอแล้วตอบ 404
$user = User::where('email', '=', 'a@example.com');
$users = User::all();
$users = User::getWhere(['status' => 'active']);
$user->name = 'ใหม่';
$user->save();
$user->delete();
$user->toArray();where / getWhere รับเฉพาะชื่อคอลัมน์ [A-Za-z_][A-Za-z0-9_]* ไม่รับ operator จากผู้ใช้ตรง ๆ
ถ้า $timestamps = true ระบบใส่ created_at / updated_at ตอนบันทึก
User::paginate($page, $perPage, ['status' => 'active']);ดูหน้า Pagination
$db = \Core\Database::getInstance()->getConnection(); // Medoo
$db->select('users', '*', ['status' => 'active']);เอกสาร Medoo: https://medoo.in/doc
Database::getInstance()->isConnected() ใช้ใน /health
Database::reconnect() บังคับต่อใหม่