Models

The Themosis framework comes with full support of the illuminate/database package and provides Laravel Eloquent ORM to your application.

Eloquent is a simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

By default, your application is configured to connect to a MySQL database. You can manage your database configuration in the config/database.php file.

In order to create a model, simply create a new PHP class inside the root app directory of your application by using the make:model console command:

php console make:model Book

The above command will generate a model class inside your app folder but you are free to place them anywhere that can be auto-loaded according to your composer.json file. All Eloquent models extends Illuminate\Database\Eloquent\Model class:

By convention, Eloquent use a "snake case", plural name of the class as the table name. So by default, our Post model expects a database posts which is the name of the table defined by WordPress.

Table name prefix is automatically handled. You can define it in your environment configuration. See the config/database.php configuration file for the available variables.

In the case where you need to customize the table name for your model, use the $table instance property like so:

protected $table = 'custom_table';

Do not specify the table prefix even for a custom name as this is handled automatically.

By default, Eloquent models expect that the primary key is an id column. However WordPress is setting the wp_posts table primary key column to ID. Let's update the default model and set the $primaryKey property of the model:

/**
* @var string
*/
protected $primaryKey = 'ID';

Each Eloquent models also expect that your database table handle timestamps using a created_at and updated_at columns. WordPress, in our case is using the post_date and post_modified columns. Let's update the model to handle those columns:

const CREATED_AT = 'post_date';
const UPDATED_AT = 'post_modified';

Here is the code sample for a custom Post model class that allow you to manage your WordPress posts using Eloquent: