Could we help you? Please click the banners. We are young and desperately need the money
Corcel is a package that allows one to easily retrieve posts, pages, or any WordPress data really, from some external environment/application using Laravel's Eloquent ORM (Object–Relational Mapper). It has built-in support for Laravel but can be used with virtually any PHP framework or project (even vanilla) that runs on top of Composer. Corcel fetches data directly from the WordPress database, which avoids running any actual WordPress logic, preventing additional overhead. In this guide, we'll show you how to quickly set up Corcel for Laravel. Note that you'll need to have Composer installed on your system. Download and install Composer here.
Inside your Laravel project directory, run the following command to install Corcel through Composer:
composer require jgrossi/corcel
Head to your Laravel project's "config/database.php" file. You'll want to add a new database connection entry there for your WordPress database. You can find the connection details in your WordPress installation's "wp-config.php" file. Here's an example of what such an entry might look like:
'corcel' => [
'driver' => 'mysql',
'host' => env('CORCEL_DB_HOST', '127.0.0.1'),
'port' => env('CORCEL_DB_PORT', '3306'),
'database' => env('CORCEL_DB_NAME', 'wordpress'),
'username' => env('CORCEL_DB_USERNAME', 'root'),
'password' => env('CORCEL_DB_PASSWORD', 'root'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => env('CORCEL_DB_PREFIX', 'wp_'),
],
If you're running your Laravel and WordPress installation locally (for local development purposes, for example), finding out the right host and port can be tricky. Here are some tips:
MySQL treats "127.0.0.1" and "localhost" as separate entities. From my experience, WordPress will connect through "localhost" while Corcel/Laravel will through "127.0.0.1" (even if "localhost" is given as the host). You may need to create another root or admin user for "127.0.0.1," for example like so:
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';
FLUSH PRIVILEGES;
If you're running Laravel through Docker (for example, via Sail) and WordPress directly on your machine, you need provide "host.docker.internal" as the host. More Docker documentation can be found here.
To find out a MySQL server's port, you can run the following query:
SHOW VARIABLES LIKE 'port';
For Laravel to be able to establish a connection to the database using Corcel, you next have to publish the Corcel configuration. This can be easily done using the following Artisan command:
php artisan vendor:publish --provider="Corcel\Laravel\CorcelServiceProvider"
Corcel can now be configured in the "config/corcel.php" file, though default settings should suffice for now.
Here are some examples on how to use Corcel:
How to fetch all published posts:
<?php
use Corcel\Model\Post;
$posts = Post::published()->get();
How to fetch all published pages:
<?php
use Corcel\Model\Page;
$pages = Page::published()->get();
How to fetch the page with the post ID "123":
<?php
use Corcel\Model\Page
$page = Page::find(123);