Code Monkey home page Code Monkey logo

wordpress-eloquent's Introduction

Wordpress Laravel Eloquent Models

A library that converts converts wordpress tables into Laravel Eloquent Models. This is helpful for dropping into any wordpress project where maybe you'd rather use the awesome features of Laravel's Eloquent Models. Or maybe you're writing an API with something like Slim or better yet Lumen don't want to increase your load time by loading the entire WP core. This is a great boiler plate based off Eloquent by Laravel to get you going.

** This is documentation for additional functionality on top of Eloquent. For documentation on all of Eloquent's features you visit the documentation.

Overview

Installation

composer require drewjbartlett/wordpress-eloquent

Setup

require_once('vendor/autoload.php');

\WPEloquent\Core\Laravel::connect([
    'global' => true,

    'config' => [

        'database' => [
            'user'     => 'user',
            'password' => 'password',
            'name'     => 'database',
            'host'     => '127.0.0.1',
            'port'     => '3306'
        ],

        // your wpdb prefix
        'prefix' => 'wp_',
    ],

    // enable events
    'events' => false,

    // enable query log
    'log'    => true
]);

If you wanted to enable this on your entire WP install you could create a file with the above code to drop in the mu-plugins folder.

Posts

use \WPEloquent\Model\Post;

// getting a post
$post = Post::find(1);

// available relationships
$post->author;
$post->comments;
$post->terms;
$post->tags;
$post->categories;
$post->meta;

Statuses

By default, the Post returns posts with all statuses. You can however override this with the local scope published to return only published posts.

Post::published()->get();

Or if you need a specific status you can override with defined status via local scope.

Post::status('draft')->get();

Post Types

By default, the Post returns posts with all post types. You can however override this by defining a post type via local scope.

Post::type('page')->get();

Comments

use \WPEloquent\Model\Comment;

// getting a comment
$comment = Comment::find(12345);

// available relationships
$comment->post;
$comment->author;
$comment->meta

Terms

In this version Term is still accesible as a model but is only leveraged through posts.

$post->terms()->where('taxonomy', 'country');

Users

use \WPEloquent\Model\User;

// getting a comment
$user = User::find(123);

// available relationships
$user->posts;
$user->meta;
$user->comments

Meta

The models Post, User, Comment, Term, all implement the HasMeta. Therefore they meta can easily be retrieved by the getMeta and set by the setMeta helper functions:

$post = Post::find(1);
$post->setMeta('featured_image', 'my-image.jpg');
$post->setMeta('breakfast', ['waffles' => 'blueberry', 'pancakes' => 'banana']);

// or all in one call
$featured_image = Post::find(1)->getMeta('featured_image');
Post::find(1)->setMeta('featured_image', 'image.jpg');

// same applies for all other models

$user = User::find(1)
$facebook = $user->getMeta('facebook');
$user->setMeta('social', ['facebook' => 'facebook.com/me', 'instagram' => 'instagram.com/me']);

$comment = Comment::find(1);
$meta = $comment->getMeta('some_comment_meta');

$term = Term::find(123);
$meta = $term->getMeta('some_term_meta');

// delete meta
$post = Post::find(123)->deleteMeta('some_term_meta');

Options

In wordpress you can use get_option. Alternatively, if you don't want to load the wordpress core you can use helper function getValue.

use \WPEloquent\Model\Post;

$siteurl = Option::getValue('siteurl');

Or of course, the long form:

use \WPEloquent\Model\Options;

$siteurl = Option::where('option_name', 'siteurl')->value('option_value');

Links

use \WPEloquent\Model\Link;

$siteurl = Link::find(1);

Extending your own models

If you want to add your own functionality to a model, for instance a User you can do so like this:

namespace App\Model;

class User extends \WPEloquent\Model\User {

    public function orders() {
        return $this->hasMany('\App\Model\User\Orders');
    }

    public function current() {
        // some functionality to get current user
    }

    public function favorites() {
        return $this->hasMany('Favorites');
    }

}

Another example would be for custom taxonomies on a post, say country

namespace App\Model;

class Post extends \WPEloquent\Model\Post {

    public function countries() {
        return $this->terms()->where('taxonomy', 'country');
    }

}

Post::with(['categories', 'countries'])->find(1);

Query Logs

Sometimes it's helpful to see the query logs for debugging. You can enable the logs by passing log is set to true (see setup) on the Laravel::connect method. Logs are retrieved by running.

use \WPEloquent\Core\Laravel;

print_r(Laravel::queryLog());

wordpress-eloquent's People

Contributors

drewjbartlett avatar riozzi avatar sandermuller avatar terence1990 avatar twosg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wordpress-eloquent's Issues

Problem installing in laravel 5.3.29

Problem 1 - Installation request for drewjbartlett/wordpress-eloquent ^0.0.2 -> satisfiable by drewjbartlett/wordpress-eloquent[v0.0.2]. - Conclusion: remove laravel/framework v5.3.29 - Conclusion: don't install laravel/framework v5.3.29 - drewjbartlett/wordpress-eloquent v0.0.2 requires illuminate/database 5.4.* -> satisfiable by illuminate/database[v5.4.0, v5.4.9]. - don't install illuminate/database v5.4.0|don't install laravel/framework v5.3.29 - don't install illuminate/database v5.4.9|don't install laravel/framework v5.3.29 - Installation request for laravel/framework (locked at v5.3.29, required as 5.3.*) -> satisfiable by laravel/framework[v5.3.29].

It would be helpful if the package had backward compability

installation failing

composer require drewjbartlett/wordpress-eloquent
Using version ^0.2.1 for drewjbartlett/wordpress-eloquent
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for drewjbartlett/wordpress-eloquent ^0.2.1 -> satisfiable by drewjbartlett/wordpress-eloquent[v0
.2.1].
- Conclusion: remove laravel/framework v5.5.20
- Conclusion: don't install laravel/framework v5.5.20

- drewjbartlett/wordpress-eloquent v0.2.1 requires illuminate/database 5.4.* -> satisfiable by illuminate/database[v5.4.

0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9].
- don't install illuminate/database v5.4.0|don't install laravel/framework v5.5.20
- don't install illuminate/database v5.4.13|don't install laravel/framework v5.5.20
- don't install illuminate/database v5.4.17|don't install laravel/framework v5.5.20
- don't install illuminate/database v5.4.19|don't install laravel/framework v5.5.20
- don't install illuminate/database v5.4.27|don't install laravel/framework v5.5.20
- don't install illuminate/database v5.4.36|don't install laravel/framework v5.5.20
- don't install illuminate/database v5.4.9|don't install laravel/framework v5.5.20
- Installation request for laravel/framework (locked at v5.5.20, required as 5.5.*) -> satisfiable by laravel/framework[
v5.5.20].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.