Sage and Corcel, what do you think?

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);

Corcel is great but I wouldn’t mix the two.

Take a look at https://github.com/pixelcollective/acorn-db – most of this can be done one way or another natively.

Thanks, this is just Mind Blowing!

I got an error running wp acorn vendor:publish

Warning: mysqli_real_connect(): (HY000/2002): No such file or directory in /www/test/web/wp/wp-includes/wp-db.php on line 1633
Error: `No such file or directory`

But I manually copied config/database.php and replaced Corcel models with AcornDb ones (and some Corcel specific methods like Post::type() using Eloquent native ones) and everything works the same.

Is it possible to use it with sage-directive loops?

This topic was automatically closed after 42 days. New replies are no longer allowed.