This is an outdated version of PHPasap. Please visit for the latest version.


Model is where you write all your database queries. Models may reside in app/models directory. Due to the namespace based autoloader in PHPasap you are free to keep your models anywhere in the project root as long as you follow proper namespace.

Example Model

Below is a User model in app/models/user.php

namespace app\Models;

class User extends \core\classes\Model{
    //it is assumed the base table for this model is user
    //which is lower case name of this class
    //if you want to explicity set the table name to something else
    //then set the table name on table property
    //protected $table = "table_name";
    public function get_user_details($user_id) {
        return $this->where("id", "=", $user_id)->first();

Below is how you would use this model in any of your controllers

namespace app\Controllers;

class Any_Controller {
    public function some_method() {
        $user_model = new app\Models\User();
        $profile = $user_model->get_user_details(5);

Table name

Note that we didn't tell the Model which table it is suppose to search form. It is assumed that the base table that model will search into will be lowercased name of the model class. So for above example it is user. If you want to use a different table then you can set the table property to the table name you want to query.

protected $table = "some_table";

Available methods

All examples listed in database section are available to your model provied your model extends the core\classes\Model. The only difference is instead of doing DB::table('table') you would use $this. For example, a SELECT query using DB class would be as follow

$all_users = DB::table("users")->get();

To do same thing in User model you would replace the DB::table("users") with $this.

$all_users = $this->get();

The table() method sets the table property. In a model this property is assumed to be same as lower case of class name (Else if tableproperty is explicity set then same will be used). So you don't need to call the table() method.