{"id":13759543,"url":"https://github.com/drewjbartlett/wordpress-eloquent","last_synced_at":"2025-04-06T12:09:43.755Z","repository":{"id":46816916,"uuid":"75991242","full_name":"drewjbartlett/wordpress-eloquent","owner":"drewjbartlett","description":"A library that converts converts wordpress tables into Laravel Eloquent Models.","archived":false,"fork":false,"pushed_at":"2021-09-23T20:22:32.000Z","size":47,"stargazers_count":150,"open_issues_count":4,"forks_count":45,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-30T11:07:17.440Z","etag":null,"topics":["laravel","laravel-eloquent-models","lumen-php-framework","oop","orm","php","slim-framework","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drewjbartlett.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-09T01:50:37.000Z","updated_at":"2025-02-24T12:42:11.000Z","dependencies_parsed_at":"2022-09-23T04:50:42.278Z","dependency_job_id":null,"html_url":"https://github.com/drewjbartlett/wordpress-eloquent","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewjbartlett%2Fwordpress-eloquent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewjbartlett%2Fwordpress-eloquent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewjbartlett%2Fwordpress-eloquent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewjbartlett%2Fwordpress-eloquent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drewjbartlett","download_url":"https://codeload.github.com/drewjbartlett/wordpress-eloquent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247478323,"owners_count":20945266,"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":["laravel","laravel-eloquent-models","lumen-php-framework","oop","orm","php","slim-framework","wordpress"],"created_at":"2024-08-03T13:00:55.113Z","updated_at":"2025-04-06T12:09:43.727Z","avatar_url":"https://github.com/drewjbartlett.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"\u003ch2\u003eWordpress Laravel Eloquent Models\u003c/h2\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"http://drewjbartlett.com/images/github/logo-laravel.svg\"\u003e\n\u003cimg src=\"http://drewjbartlett.com/images/github/wordpress-logo.png\"\u003e\n\u003c/p\u003e\n\nA library that converts converts wordpress tables into [Laravel Eloquent Models](https://laravel.com/docs/5.3/eloquent). 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](https://www.slimframework.com/) or better yet [Lumen](https://lumen.laravel.com/) don't want to increase your load time by loading the entire WP core. This is a great boiler plate based off [Eloquent](https://laravel.com/docs/5.3/eloquent) by Laravel to get you going.\n\n** This is documentation for additional functionality on top of Eloquent. For documentation on all of Eloquent's features you visit the [documentation](https://laravel.com/docs/5.3/eloquent).\n\n## Overview\n - [Installation](#installation)\n - [Setup](#setup)\n - [Posts](#posts)\n - [Comments](#comments)\n - [Terms](#terms)\n - [Users](#users)\n - [Meta](#meta)\n - [Options](#options)\n - [Links](#links)\n - [Extending your own models](#extending-your-own-models)\n - [Query Logs](#query-logs)\n\n### Installation\n\n    composer require drewjbartlett/wordpress-eloquent\n\n### Setup\n\n```php\nrequire_once('vendor/autoload.php');\n\n\\WPEloquent\\Core\\Laravel::connect([\n    'global' =\u003e true,\n\n    'config' =\u003e [\n\n        'database' =\u003e [\n            'user'     =\u003e 'user',\n            'password' =\u003e 'password',\n            'name'     =\u003e 'database',\n            'host'     =\u003e '127.0.0.1',\n            'port'     =\u003e '3306'\n        ],\n\n        // your wpdb prefix\n        'prefix' =\u003e 'wp_',\n    ],\n\n    // enable events\n    'events' =\u003e false,\n\n    // enable query log\n    'log'    =\u003e true\n]);\n\n```\n\nIf 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.\n\n### Posts\n\n```php\n\nuse \\WPEloquent\\Model\\Post;\n\n// getting a post\n$post = Post::find(1);\n\n// available relationships\n$post-\u003eauthor;\n$post-\u003ecomments;\n$post-\u003eterms;\n$post-\u003etags;\n$post-\u003ecategories;\n$post-\u003emeta;\n\n```\n\n***Statuses***\n\nBy default, the `Post` returns posts with all statuses. You can however override this with the [local scope](https://laravel.com/docs/5.3/eloquent#query-scopes) `published` to return only published posts.\n\n```php\nPost::published()-\u003eget();\n```\n\nOr if you need a specific status you can override with defined status via [local scope](https://laravel.com/docs/5.3/eloquent#query-scopes).\n\n```php\nPost::status('draft')-\u003eget();\n```\n\n***Post Types***\n\nBy default, the `Post` returns posts with all post types. You can however override this by defining a post type via [local scope](https://laravel.com/docs/5.3/eloquent#query-scopes).\n\n```php\nPost::type('page')-\u003eget();\n```\n\n### Comments\n\n```php\n\nuse \\WPEloquent\\Model\\Comment;\n\n// getting a comment\n$comment = Comment::find(12345);\n\n// available relationships\n$comment-\u003epost;\n$comment-\u003eauthor;\n$comment-\u003emeta\n\n```\n\n### Terms\n\nIn this version `Term` is still accesible as a model but is only leveraged through posts.\n\n```php\n$post-\u003eterms()-\u003ewhere('taxonomy', 'country');\n```\n\n### Users\n\n```php\n\nuse \\WPEloquent\\Model\\User;\n\n// getting a comment\n$user = User::find(123);\n\n// available relationships\n$user-\u003eposts;\n$user-\u003emeta;\n$user-\u003ecomments\n\n```\n\n### Meta\n\nThe 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:\n\n```php\n$post = Post::find(1);\n$post-\u003esetMeta('featured_image', 'my-image.jpg');\n$post-\u003esetMeta('breakfast', ['waffles' =\u003e 'blueberry', 'pancakes' =\u003e 'banana']);\n\n// or all in one call\n$featured_image = Post::find(1)-\u003egetMeta('featured_image');\nPost::find(1)-\u003esetMeta('featured_image', 'image.jpg');\n\n// same applies for all other models\n\n$user = User::find(1)\n$facebook = $user-\u003egetMeta('facebook');\n$user-\u003esetMeta('social', ['facebook' =\u003e 'facebook.com/me', 'instagram' =\u003e 'instagram.com/me']);\n\n$comment = Comment::find(1);\n$meta = $comment-\u003egetMeta('some_comment_meta');\n\n$term = Term::find(123);\n$meta = $term-\u003egetMeta('some_term_meta');\n\n// delete meta\n$post = Post::find(123)-\u003edeleteMeta('some_term_meta');\n```\n\n### Options\n\nIn wordpress you can use `get_option`. Alternatively, if you don't want to load the wordpress core you can use helper function `getValue`.\n\n```php\nuse \\WPEloquent\\Model\\Post;\n\n$siteurl = Option::getValue('siteurl');\n```\n\nOr of course, the long form:\n\n```php\nuse \\WPEloquent\\Model\\Options;\n\n$siteurl = Option::where('option_name', 'siteurl')-\u003evalue('option_value');\n```\n\n\n### Links\n\n```php\nuse \\WPEloquent\\Model\\Link;\n\n$siteurl = Link::find(1);\n```\n\n### Extending your own models\n\nIf you want to add your own functionality to a model, for instance a `User` you can do so like this:\n\n```php\nnamespace App\\Model;\n\nclass User extends \\WPEloquent\\Model\\User {\n\n    public function orders() {\n        return $this-\u003ehasMany('\\App\\Model\\User\\Orders');\n    }\n\n    public function current() {\n        // some functionality to get current user\n    }\n\n    public function favorites() {\n        return $this-\u003ehasMany('Favorites');\n    }\n\n}\n```\n\nAnother example would be for custom taxonomies on a post, say `country`\n\n```php\nnamespace App\\Model;\n\nclass Post extends \\WPEloquent\\Model\\Post {\n\n    public function countries() {\n        return $this-\u003eterms()-\u003ewhere('taxonomy', 'country');\n    }\n\n}\n\nPost::with(['categories', 'countries'])-\u003efind(1);\n```\n\n### Query Logs\n\nSometimes it's helpful to see the query logs for debugging. You can enable the logs by passing `log` is set to `true` (see [setup](#setup)) on the `Laravel::connect` method. Logs are retrieved by running.\n\n```php\nuse \\WPEloquent\\Core\\Laravel;\n\nprint_r(Laravel::queryLog());\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrewjbartlett%2Fwordpress-eloquent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrewjbartlett%2Fwordpress-eloquent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrewjbartlett%2Fwordpress-eloquent/lists"}