I’m on Sage 10 exciting of explore the Laravel world.
I’m trying Corcel as Eloquent wrapper of WP Models.
The main idea is to have an elegant way to query the db without unnecessary extra stuff.
The second concern is about performance and cache.
I’m not planning to use it for loops.
What do you think?
Here is an example:
<?php
namespace App\Composers\Partials;
use Roots\Acorn\View\Composer;
use Corcel\Model\Post;
class ContentCollection extends Composer
{
protected static $views = [
'partials.content-collection'
];
public function with()
{
global $post;
return [
'collectionsCount' => Post::type('collection')->where('post_parent', $post->ID)->get()->count(),
'productsCount' => Post::type('product')->where('post_parent', $post->ID)->get()->count()
];
}
}
To register the library you should edit functions.php
/**
* Ensure dependencies are loaded.
*/
if (! file_exists($composer = __DIR__ . '/vendor/autoload.php')) {
$sage_error(
__('You must run <code>composer install</code> from the Sage directory.', 'sage'),
__('Autoloader not found.', 'sage')
);
}
require_once $composer;
/**
* Register Corcel
*/
global $wpdb;
$params = array(
'database' => Config::get('DB_NAME'),
'username' => Config::get('DB_USER'),
'password' => Config::get('DB_PASSWORD'),
'prefix' => $wpdb->prefix
);
Corcel\Database::connect($params);