06
SepCorcel is a collection of PHP classes built on top of Eloquent ORM (from Laravel framework), that provides a fluent interface to connect and get data directly from a WordPress database.
You can use WordPress as the backend (administration panel) or CMS, for inserting posts, custom types, etc, and any other PHP app on the other side querying those data (as a Model layer). It’s easier to use Corcel with Laravel, but you’re free to use it with any PHP project that uses Composer.
You need to use Composer to install Corcel into your project:
composer require jgrossi/corcel
Now publish the configurations:
php artisan vendor:publish --provider="Corcel\Laravel\CorcelServiceProvider"
Posts
Every time you see Post::method()
, if you’re using your own Post
class (where you set the connection name), like App\Post
you should use App\Post::method()
and not Post::method()
. All the examples are assuming you already know this difference.
In the examples, every time you see Post::method()
assume Corcel\Model\Post::method()
.
// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();
// A specific post
$post = Post::find(31);
echo $post->post_title;
Creating your own model classes
Optionally you can create your own Post
model (or Page, or whatever) which extends Corcel\Post
. Then set the connection name (if you want to override the Corcel’s default one) you’re using, in this case, foo-bar
:
Extending Corcel\Model\Post
class can add flexibility to your project, once you can add custom methods and logic, according to what you need to use from your WordPress database.
<?php // File: app/Post.php
namespace App;
use Corcel\Model\Post as Corcel;
class Post extends Corcel
{
protected $connection = 'foo-bar';
public function customMethod() {
//
}
}
So, now you can fetch WP database data using your own class:
$posts = App\Post::all(); // using the 'foo-bar' connection
Just remember you don’t have to extend our Post class, you can use Corcel\Model\Post
and all other models without any problem.
Meta Data (Custom Fields)
NOTE: In Corcel v1 you could save metadata using the Post::save()
method. That’s not allowed anymore. Use saveMeta()
or createMeta()
(see below) methods to save post meta.
You can retrieve metadata from posts too.
// 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
To create or update metadata form a User just use the saveMeta()
or saveField()
methods. They return bool like the Eloquent save()
method.
$post = Post::find(1);
$post->saveMeta('username', 'jgrossi');
You can save many metadata at the same time too:
$post = Post::find(1);
$post->saveMeta([
'username' => 'jgrossi',
'url' => 'http://jgrossi.com',
]);
You also have the createMeta()
and createField()
methods, which work like the saveX()
methods, but they are used only for creation and return the PostMeta
created instance, instead of bool.
$post = Post::find(1);
$postMeta = $post->createMeta('foo', 'bar'); // instance of PostMeta class
$trueOrFalse = $post->saveMeta('foo', 'baz'); // boolean
Custom Scopes
To order posts you can use newest()
and oldest() scopes, for both Post and User classes:
$newest = Post::newest()->first();
$oldest = Post::oldest()->first();
Pagination
To order posts just use the Eloquent paginate()
method:
$posts = Post::published()->paginate(5);
foreach ($posts as $post) {
// ...
}
To display the pagination links just call the links() method:
{{ $posts->links() }}
This package has a lot of amazing features, If you wanna know more, then please visit its complete documentation on Github.
Published at : 06-09-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project