📦 Models in Squehub
Models in Squehub represent the structure of your database tables. They provide a clean way to query, insert, update, and delete records using PHP code.
All models are stored in the project/models/ directory and extend App\Core\Model, which handles the database logic internally.
📁 Directory Structure
project/
└── models/
└── User.php
🧩 Full User Model Example
This is a complete example of a typical model for managing users.
<?php
namespace Project\Models;
use App\Core\Model;
class User extends Model
{
protected static $table = 'users';
public static function createUser($name, $username, $email, $password, $country)
{
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
return self::create([
'name' => $name,
'username' => $username,
'email' => $email,
'password' => $hashedPassword,
'country' => $country,
'status' => 'active',
]);
}
public static function deleteUserByEmail($email)
{
$user = self::where('email', $email);
if ($user) {
return self::delete($user['id']);
}
return false;
}
public static function emailExists($email)
{
return !empty(self::where('email', $email));
}
public static function usernameExists($username)
{
return !empty(self::where('username', $username));
}
public static function getUserById($id)
{
return self::find($id);
}
public static function getUsersByCountry($country)
{
return self::where('country', $country);
}
public static function getUsersByStatus($status)
{
return self::whereAll('status', $status);
}
public static function getAllUsers()
{
return self::all();
}
}
📖 Line-by-Line Explanation
namespace Project\Models;– Declares the namespace for the model.use App\Core\Model;– Imports the base Model class that provides all the database methods.class User extends Model– Defines the User model, which inherits all base model capabilities.protected static $table = 'users';– Specifies the database table to use (i.e.,users).
🧰 Core Model Methods (Available to All Models)
all()– Returns all rows from the table. Use when you need every record.find($id)– Find a record by primary key (defaultid).create(array $data)– Insert a new record. Accepts associative array of column => value.update($id, array $data)– Update a record by ID. Returns the number of affected rows.delete($id)– Delete a record by primary key.where($column, $value)– Fetch the first row matching a condition.whereFirst($column, $value)– Alias ofwhere()withLIMIT 1.whereAll($column, $value)– Fetch all rows that match a condition.
📝 You must define
protected static $table = 'your_table'; in every model to use these methods.
✅ Summary
- Models extend
App\Core\Modeland live inproject/models/. - They interact with database tables defined by
$table. - You can build custom methods (like
createUser()) that call core ones internally. - Available core methods:
all(),find(),create(),update(),delete(),where(),whereFirst(),whereAll(). - Use
wherefor one record,whereAllfor many. - Use
createandupdatein forms or seeders.
💡 Tip: Define business logic (e.g.,
createUser, isActive, getAdmins) inside your model methods. This keeps controllers clean and your logic reusable.