Skip to content

Instantly share code, notes, and snippets.

@jgrossi jgrossi/Car.php
Last active Jul 3, 2018

Embed
What would you like to do?
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 -->
@mansouralex

This comment has been minimized.

Copy link

commented Feb 18, 2016

Please add "illuminate/events": "~5.0.0" It's needed for the Dispatcher.

@balachandranmca

This comment has been minimized.

Copy link

commented Nov 18, 2016

Good Job 👍

@balachandranmca

This comment has been minimized.

Copy link

commented Nov 18, 2016

Please remove comma from composer.json and change App/Models/Car::all(); to App\Models\Car::all(); in page.php

@tfevan

This comment has been minimized.

Copy link

commented Jan 12, 2017

Amazing !

@Mrstolen

This comment has been minimized.

Copy link

commented Jul 3, 2018

Hey, I'm trying to integrate this in a wordpress plugin, but after doing it I'm having an error:

Uncaught Error: Class 'App\Models\Route' not found in (.....)

I think everything is fine, I made some small changes:

I installed composer files (/vendor) in the plugIn folder, as so, I changed the composer.json to be:
{ "require": { "illuminate/database": "~5.0.0", "illuminate/events": "~5.0.0" }, "autoload": { "psr-4": { "App\\": "" } } }

next I created a Modals folder with my Route.php inside:

`namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Route extends Eloquent
{
...
}
`

My "page.php" has the following:

`add_shortcode( 'pluginTestMiguel', 'plugin_test' );

function plugin_test(){

$routes = App\Models\Route::all();

foreach($routes as $route):
    echo $route->title;
endforeach;

echo file_get_contents(dirname(__FILE__) . "/html/testHTML.html");

}`
Even without the shortcode/function the error stays

I could paste my functions.php but I don't think it can be that

When I'm writing code, the IDE autocompletes the namespace ( App\Models\Route) so I don't know how it can't be found when I run it.

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.