Skip to content

Query Builder

The Model class is the base model class that provides a common interface for interacting with the database. It's an abstract class, so it can't be instantiated on its own. Instead, it's meant to be extended by specific models.

The class has several protected properties and methods that subclasses can use to interact with the database. Here's an overview of each property and method:

Properties

  • protected static $table: This property holds the name of the database table associated with the model. By default, the property is set to the lowercase version of the class name followed by an "s" (e.g. User => users).

  • protected static $fillable: This property holds an array of columns that are allowed to be filled by the insert and update methods. If this array is empty, all columns are allowed to be filled.

  • protected static $primaryKey: This property holds the name of the primary key column for the model. By default, it's set to 'id'.

  • private static $wheres: This property holds an array of WHERE clauses for the query.

  • private static $orderBys: This property holds an array of ORDER BY clauses for the query.

  • private static $groupBy: This property holds the name of the column to GROUP BY.

  • private static $limit: This property holds the maximum number of records to return.

  • private static $offset: This property holds the number of records to skip.

  • private static $innerJoin: This property holds an array of INNER JOIN clauses for the query.

  • private static $whereBeetween: This property holds an array of WHERE BETWEEN clauses for the query.

Methods & Usage

  • public static function all(): This method returns all records from the associated database table.

    $users = User::all();
    
  • public static function find($id): This method returns a record from the associated database table by its primary key value.

    $user = User::find(1);
    
  • public static function insert($data): This method inserts a new record into the associated database table. The $data parameter is an associative array of column names and values to be inserted.

    User::insert($_POST);
    
  • public static function update($id, $data): This method updates an existing record in the associated database table by its primary key value. The $id parameter is the primary key value of the record to update, and the $data parameter is an associative array of column names and values to be updated.

    User::update(1, $_POST);
    
  • public static function delete($id): This method deletes a record from the associated database table by its primary key value.

    User::delete(1);
    
  • public static function where($column, $operator, $value, $condition = 'AND'): This method adds a WHERE clause to the query.

    User::where('id', '=', 1);
    User::where('id', '=', 1)
        ->where('name', '=', 'John Doe')
        ->get();
    
  • public static function orderBy($column, $direction = 'ASC'): This method adds an ORDER BY clause to the query.

    User::where('id', '=', 1)
        ->where('name', '=', 'John Doe')
        ->orderBy('name', 'DESC')
        ->get();
    
  • public static function groupBy($column): This method adds a GROUP BY clause to the query.

    User::where('id', '=', 1)
        ->where('name', '=', 'John Doe')
        ->groupBy('name')
        ->get();
    
  • public static function limit($limit): This method sets the maximum number of records to return.

    User::limit(10)->get();
    
  • public static function offset($offset): This method sets the number of records to skip.

    User::offset(20)->limit(10)->get();
    
  • public static function innerJoin($table, $first, $operator, $second, $type = 'INNER'): This method adds an INNER JOIN clause to the query.

    User::innerJoin("profile", "id", "user_id")->get();
    
  • public static function whereBetween($column, $min, $max, $condition = 'AND'): This method adds a WHERE BETWEEN clause to the query.

    User::whereBetween('id', 1, 10)->get();