Skip to content

Instantly share code, notes, and snippets.

@raidenz
Forked from jgrossi/Car.php
Created August 25, 2016 15:42
Show Gist options
  • Save raidenz/d3d014014f0f2f37fff7b535a4283e3c to your computer and use it in GitHub Desktop.
Save raidenz/d3d014014f0f2f37fff7b535a4283e3c to your computer and use it in GitHub Desktop.
How to use Eloquent (from Laravel) inside Wordpress
<?php // File location: /wp-content/themes/my-theme/src/Models/
namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Car extends Eloquent
{
protected $table = 'cars';
protected $primaryId = 'id';
}
{
"require": {
"illuminate/database": "~5.0.0"
},
"autoload": {
"psr-4": {
"App\\": "wp-content/themes/my-theme/src/"
}
},
}
<?php // File location: /wp-content/themes/your-theme/
require __DIR__.'/../../../vendor/autoload.php'; // include composer inside Wordpress
/*
* Configure Eloquent (called Capsule when used alone)
*/
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setEventDispatcher(new \Illuminate\Events\Dispatcher(new \Illuminate\Container\Container));
$capsule->setAsGlobal();
$capsule->bootEloquent();
// other code in functions.php here
<?php // File location: /wp-content/themes/my-theme/
$cars = App/Models/Car::all(); ?>
<!-- HTML or PHP code -->
<?php foreach ($cars as $car): ?>
<h1><?php echo $car->model_name ?></h1>
<?php endforeach; ?>
<!-- HTML or PHP code -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment