-
-
Save raidenz/d3d014014f0f2f37fff7b535a4283e3c to your computer and use it in GitHub Desktop.
How to use Eloquent (from Laravel) inside Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"illuminate/database": "~5.0.0" | |
}, | |
"autoload": { | |
"psr-4": { | |
"App\\": "wp-content/themes/my-theme/src/" | |
} | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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