PHP code example of wenprise / eloquent

1. Go to this page and download the library: Download wenprise/eloquent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wenprise / eloquent example snippets


$db = \Wenprise\Eloquent\Connection::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 \Wenprise\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() );

use Wenprise\ORM\WP\Post;

// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();

// A specific post
$post = Post::find(31);
echo $post->post_title;

// Find a page by slug
$page = Page::slug('about')->first(); // OR
$page = Post::type('page')->slug('about')->first();
echo $page->post_title;

use Wenprise\ORM\WP\Comment;

// Get Comment with id 12345
$comment = Comment::find(12345);

// Get related data
$comment->post;
$comment->author;
$comment->meta

// Get a custom meta value (like 'link' or whatever) from a post (any type)
$post = Post::find(31);
echo $post->meta->link; // OR
echo $post->fields->link;
echo $post->link; // OR

$post = Post::find(1);
$post->saveMeta('username', 'jgrossi');

$post = Post::find(1);
$post->saveMeta([
    'username' => 'jgrossi',
    'url' => 'http://jgrossi.com',
]);

$post = Post::find(1);
$postMeta = $post->createMeta('foo', 'bar'); // instance of PostMeta class
$trueOrFalse = $post->saveMeta('foo', 'baz'); // boolean

// Using just one custom field
$post = Post::published()->hasMeta('username', 'jgrossi')->first(); // setting key and value
$post = Post::published()->hasMeta('username'); // setting just the key

$post = Post::hasMeta(['username' => 'jgrossi'])->first();
$post = Post::hasMeta(['username' => 'jgrossi', 'url' => 'jgrossi.com'])->first();
// Or just passing the keys
$post = Post::hasMeta(['username', 'url'])->first();

$post = Post::find(1);
$post->title === $post->post_title; // true

class A extends Post
{
    protected static $aliases = [
        'foo' => 'post_foo',
    ];
}

$a = A::find(1);
echo $a->foo;
echo $a->title; // from Post class

$newest = Post::newest()->first();
$oldest = Post::oldest()->first();

$posts = Post::published()->paginate(5);
foreach ($posts as $post) {
    // ...
}

 {{ $posts->links() }}
 

$post = Post::find(1);
$taxonomy = $post->taxonomies()->first();
echo $taxonomy->taxonomy;

$post = Post::taxonomy('category', 'php')->first();

// all categories
$cat = Taxonomy::category()->slug('uncategorized')->first()->posts();
echo "<pre>"; print_r($cat->name); echo "</pre>";

// only all categories and posts connected with it
$cat = Taxonomy::where('taxonomy', 'category')->with('posts')->get();
$cat->each(function($category) {
    echo $category->name;
});

$page = Page::slug('about')->with('attachment')->first();
// get feature image from page or post
print_r($page->attachment);

$post = Post::slug('test')->with('revision')->first();
// get all revisions from a post or page
print_r($post->revision);

// All users
$users = User::get();

// A specific user
$user = User::find(1);
echo $user->user_login;

$siteUrl = Option::get('siteurl');

Option::add('foo', 'bar'); // stored as string
Option::add('baz', ['one' => 'two']); // this will be serialized and saved

$options = Option::asArray();
echo $options['siteurl'];

$options = Option::asArray(['siteurl', 'home', 'blogname']);
echo $options['home'];

use \Wenprise\Eloquent\Model;

class Employee extends Model {

    /**
    * Name for table without prefix, the model can automatic add it
    *
    * @var string
    */
    protected $table = 'table_name';
    
    /**
    * Columns that can be edited
    *
    * @var array
    */
    protected $fillable = [];
    
    /**
    * Disable created_at and update_at columns, unless you have those.
    */
	 public $timestamps = false;
	 
    /**
    * 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' ];
    
    /**
     * The column names allow to be filled 
     * @var array
     */
    protected $fillable = [
        'name',
        'status',
    ];

}

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