Need help with wp-eloquent?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

tareq1988
469 Stars 96 Forks 58 Commits 37 Opened issues

Description

Eloquent ORM for WordPress

Services available

!
?

Need anything else?

Contributors list

# 79,176
PHP
WordPre...
Less
hr
18 commits
# 23,024
PHP
WordPre...
JavaScr...
hr
5 commits
# 278,672
eloquen...
Databas...
MySQL
WordPre...
3 commits
# 324,771
PHP
eloquen...
Databas...
MySQL
2 commits
# 9,799
PHP
WordPre...
Laravel
php7
1 commit
# 230,879
woocomm...
HTML
JavaScr...
WordPre...
1 commit
# 141,873
WordPre...
JavaScr...
Less
account...
1 commit
# 196,646
PHP
JavaScr...
l10n
WordPre...
1 commit
# 391,089
PHP
eloquen...
Databas...
MySQL
1 commit
# 229,704
JavaScr...
HTML
chrome-...
screen-...
1 commit
# 283,798
eloquen...
MySQL
WordPre...
Less
1 commit
# 392,315
PHP
eloquen...
Databas...
MySQL
1 commit
# 143,462
JavaScr...
Less
WordPre...
hr
1 commit
# 33,079
npm
cdn
jsdeliv...
Laravel
1 commit
# 390,336
PHP
eloquen...
Databas...
MySQL
1 commit

Eloquent Wrapper for WordPress

This is a library package to use Laravel's Eloquent ORM with WordPress.

Package Installation

To install this package, edit your

composer.json
file:
{
    "require": {
        "tareq1988/wp-eloquent": "dev-master"
    }
}

Now run:

$ composer install

Usage Example

Basic Usage

$db = \WeDevs\ORM\Eloquent\Database::instance();

var_dump( $db->table('users')->find(1) ); var_dump( $db->select('SELECT * FROM wp_users WHERE id = ?', [1]) ); var_dump( $db->table('users')->where('user_login', 'john')->first() );

// OR with DB facade use \WeDevs\ORM\Eloquent\Facades\DB;

var_dump( DB::table('users')->find(1) ); var_dump( DB::select('SELECT * FROM wp_users WHERE id = ?', [1]) ); var_dump( DB::table('users')->where('user_login', 'john')->first() );

Creating Models For Custom Tables

You can use custom tables of the WordPress databases to create models:

use WeDevs\ORM\Eloquent\Model;

class CustomTableModel extends Model {

/**
 * Name for table without prefix
 *
 * @var string
 */
protected $table = 'table_name';

/**
 * Columns that can be edited - IE not primary key or timestamps if being used
 */
protected $fillable = [
    'city',
    'state',
    'country'
];

/**
 * Disable created_at and update_at columns, unless you have those.
 */
public $timestamps = false;

/** Everything below this is best done in an abstract class that custom tables extend */

/**
 * Set primary key as ID, because WordPress
 *
 * @var string
 */
protected $primaryKey = 'ID';

/**
 * Make ID guarded -- without this ID doesn't save.
 *
 * @var string
 */
protected $guarded = [ 'ID' ];

/**
 * Overide parent method to make sure prefixing is correct.
 *
 * @return string
 */
public function getTable()
{
    // In this example, it's set, but this is better in an abstract class
    if ( isset( $this->table ) ){
        $prefix =  $this->getConnection()->db->prefix;

        return $prefix . $this->table;
    }

    return parent::getTable();
}

}

Retrieving All Rows From A Table

$users = $db->table('users')->get();

foreach ($users as $user) { var_dump($user->display_name); }

Here

users
is the table name without prefix. The prefix will be applied automatically.

Other Examples

Writing a Model

use \WeDevs\ORM\Eloquent\Model as Model;

class Employee extends Model {

}

var_dump( Employee::all()->toArray() ); // gets all employees var_dump( Employee::find(1) ); // find employee with ID 1

The class name

Employee
will be translated into
PREFIX_employees
table to run queries. But as usual, you can override the table name.

In-built Models for WordPress

  • Post
  • Comment
  • Post Meta
  • User
  • User Meta
use WeDevs\ORM\WP\Post as Post;

var_dump( Post::all() ); //returns only posts with WordPress post_type "post"

Filter
Post
by
post_status
and
post_type

use WeDevs\ORM\WP\Post as Post;
var_dump(Post::type('page')->get()->toArray()); // get pages
var_dump(Post::status('publish')->get()->toArray()); // get posts with publish status
var_dump(Post::type('page')->status('publish')->get()->toArray()); // get pages with publish status

How it Works

  • Eloquent is mainly used here as the query builder
  • WPDB is used to run queries built by Eloquent
  • Hence, we have the benfit to use plugins like
    debug-bar
    or
    query-monitor
    to get SQL query reporting.
  • It doesn't create any extra MySQL connection

Minimum Requirement

  • PHP 5.6.4+
  • WordPress 4.0+

Author

Tareq Hasan

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.