Historical v1.x reference.
These examples describe the original API. Read the current v2.x guides
or the upgrade guide.
π¦ 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.

