{"id":13527845,"url":"https://github.com/tareq1988/wp-eloquent","last_synced_at":"2025-05-15T17:03:44.972Z","repository":{"id":27197754,"uuid":"30668150","full_name":"tareq1988/wp-eloquent","owner":"tareq1988","description":"Eloquent ORM for WordPress","archived":false,"fork":false,"pushed_at":"2022-05-11T12:18:30.000Z","size":52,"stargazers_count":570,"open_issues_count":43,"forks_count":138,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-05-11T05:16:09.170Z","etag":null,"topics":["database","eloquent","mysql","orm","wordpress"],"latest_commit_sha":null,"homepage":"https://tareq.co/2015/05/eloquent-orm-in-wordpress/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tareq1988.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-11T20:28:19.000Z","updated_at":"2025-05-04T05:30:02.000Z","dependencies_parsed_at":"2022-08-07T14:00:07.752Z","dependency_job_id":null,"html_url":"https://github.com/tareq1988/wp-eloquent","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tareq1988%2Fwp-eloquent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tareq1988%2Fwp-eloquent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tareq1988%2Fwp-eloquent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tareq1988%2Fwp-eloquent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tareq1988","download_url":"https://codeload.github.com/tareq1988/wp-eloquent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384982,"owners_count":22062422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","eloquent","mysql","orm","wordpress"],"created_at":"2024-08-01T06:02:03.478Z","updated_at":"2025-05-15T17:03:44.944Z","avatar_url":"https://github.com/tareq1988.png","language":"PHP","funding_links":[],"categories":["PHP","Developer Tools \u0026 Libraries"],"sub_categories":["🚀 How to contribute"],"readme":"# Eloquent Wrapper for WordPress\n\nThis is a library package to use Laravel's [Eloquent ORM](http://laravel.com/docs/5.0/eloquent) with WordPress.\n\n\n## Package Installation\n\nTo install this package, edit your `composer.json` file:\n\n```js\n{\n    \"require\": {\n        \"tareq1988/wp-eloquent\": \"dev-master\"\n    }\n}\n```\n\nNow run:\n\n`$ composer install`\n\n# Usage Example\n\n## Basic Usage\n\n```php\n\n$db = \\WeDevs\\ORM\\Eloquent\\Database::instance();\n\nvar_dump( $db-\u003etable('users')-\u003efind(1) );\nvar_dump( $db-\u003eselect('SELECT * FROM wp_users WHERE id = ?', [1]) );\nvar_dump( $db-\u003etable('users')-\u003ewhere('user_login', 'john')-\u003efirst() );\n\n// OR with DB facade\nuse \\WeDevs\\ORM\\Eloquent\\Facades\\DB;\n\nvar_dump( DB::table('users')-\u003efind(1) );\nvar_dump( DB::select('SELECT * FROM wp_users WHERE id = ?', [1]) );\nvar_dump( DB::table('users')-\u003ewhere('user_login', 'john')-\u003efirst() );\n```\n\n## Creating Models For Custom Tables\nYou can use custom tables of the WordPress databases to create models:\n\n```php\n\u003c?php\nnamespace Whatever;\n\nuse WeDevs\\ORM\\Eloquent\\Model;\n\nclass CustomTableModel extends Model {\n\n    /**\n     * Name for table without prefix\n     *\n     * @var string\n     */\n    protected $table = 'table_name';\n\n    /**\n     * Columns that can be edited - IE not primary key or timestamps if being used\n     */\n    protected $fillable = [\n        'city',\n        'state',\n        'country'\n    ];\n\n    /**\n     * Disable created_at and update_at columns, unless you have those.\n     */\n    public $timestamps = false;\n\n    /** Everything below this is best done in an abstract class that custom tables extend */\n\n    /**\n     * Set primary key as ID, because WordPress\n     *\n     * @var string\n     */\n    protected $primaryKey = 'ID';\n\n    /**\n     * Make ID guarded -- without this ID doesn't save.\n     *\n     * @var string\n     */\n    protected $guarded = [ 'ID' ];\n\n    /**\n     * Overide parent method to make sure prefixing is correct.\n     *\n     * @return string\n     */\n    public function getTable()\n    {\n        // In this example, it's set, but this is better in an abstract class\n        if ( isset( $this-\u003etable ) ){\n            $prefix =  $this-\u003egetConnection()-\u003edb-\u003eprefix;\n            \n            return $prefix . $this-\u003etable;\n        }\n\n        return parent::getTable();\n    }\n\n}\n```\n\n### Retrieving All Rows From A Table\n\n```php\n$users = $db-\u003etable('users')-\u003eget();\n\nforeach ($users as $user) {\n    var_dump($user-\u003edisplay_name);\n}\n```\n\nHere `users` is the table name **without prefix**. The prefix will be applied automatically.\n\n\n### Other Examples\n\n - [Queries](http://laravel.com/docs/5.0/queries)\n - [Eloquent ORM](http://laravel.com/docs/5.0/eloquent)\n\n## Writing a Model\n\n```php\nuse \\WeDevs\\ORM\\Eloquent\\Model as Model;\n\nclass Employee extends Model {\n\n}\n\nvar_dump( Employee::all()-\u003etoArray() ); // gets all employees\nvar_dump( Employee::find(1) ); // find employee with ID 1\n```\nThe class name `Employee` will be translated into `PREFIX_employees` table to run queries. But as usual, you can override the table name.\n\n### In-built Models for WordPress\n\n- Post\n- Comment\n- Post Meta\n- User\n- User Meta\n\n\n```php\nuse WeDevs\\ORM\\WP\\Post as Post;\n\nvar_dump( Post::all() ); //returns only posts with WordPress post_type \"post\"\n```\n\n#### Filter `Post` by `post_status` and `post_type`\n```php\nuse WeDevs\\ORM\\WP\\Post as Post;\nvar_dump(Post::type('page')-\u003eget()-\u003etoArray()); // get pages\nvar_dump(Post::status('publish')-\u003eget()-\u003etoArray()); // get posts with publish status\nvar_dump(Post::type('page')-\u003estatus('publish')-\u003eget()-\u003etoArray()); // get pages with publish status\n```\n\n## How it Works\n\n - Eloquent is mainly used here as the query builder\n - [WPDB](http://codex.wordpress.org/Class_Reference/wpdb) is used to run queries built by Eloquent\n - Hence, we have the benfit to use plugins like `debug-bar` or `query-monitor` to get SQL query reporting.\n - It doesn't create any extra MySQL connection\n\n\n## Minimum Requirement\n - PHP 5.6.4+\n - WordPress 4.0+\n\n## Author\n[Tareq Hasan](https://tareq.co)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftareq1988%2Fwp-eloquent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftareq1988%2Fwp-eloquent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftareq1988%2Fwp-eloquent/lists"}