-
-
Save jgrossi/1e9858c24e216112556e to your computer and use it in GitHub Desktop.
<?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 --> |
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
FYI you don't need to redefine your database connection settings since it's already in wp-config.php. Just do this:
$capsule->addConnection([
'driver' => 'mysql',
'host' => DB_HOST,
'database' => DB_NAME,
'username' => DB_USER,
'password' => DB_PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
Please use this package
https://github.com/tareq1988/wp-eloquent
in this package we use the direct connection of the wpdb class pdo.
connecting directly to the capsule is an unnecessary connection to the database
thanks you @tareq1988
Please use this package https://github.com/tareq1988/wp-eloquent in this package we use the direct connection of the wpdb class pdo. connecting directly to the capsule is an unnecessary connection to the database thanks you @tareq1988
Sure would be nice if tareq1988/wp-eloquent was kept up to date. It hasn't been updated in ~2 years and is not PHP 7.4 compatible which is almost at its EOL. I know you wrote that comment about 2 years ago as well. To move forward with PHP 8 we will need another solution.
I haven't updated the tareq1988/wp-eloquent
because Eloquent is very slow. It takes a while to generate a query, let alone query the database. Also, Eloquent is used alongside of Laravel, which supports the latest PHP version all the time. But for WordPress plugins that's not the case. We have to support even PHP 5.6, which makes it a nightmare to keep up with the original package. That's why it didn't make sense to update the library.
Amazing !