You get a bonus - 1 coin for daily activity. Now you have 1 coin

How use Database Eloquent in a standalone file without Laravel

Practice



1) composer install "illuminate/database"
2) for use Eloquent into independed file need:
require __DIR__.'/../../../bootstrap/autoload.php';
$app = require_once __DIR__.'/../../../bootstrap/app.php';
$configDB =require_once __DIR__.'/../config/database.php';
require_once __DIR__.'/../../../app/Models/SomeModel.php' ;
\App\Models\SomeModel::initConnection($configDB['connections']['mysql']);
$image = \App\Models\SomeModel::where('id',,1)->first();
var_dump(image);
3) iside model to add

namespace App\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Capsule\Manager as Capsule;
class SomeModelextends Eloquent
{
public $table = "table";

static function initConnection($configDB) {
$capsule = new Capsule;
$capsule->addConnection($configDB);
// Setup the Eloquent ORM…
$capsule->bootEloquent();
}

}

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Scripting client side JavaScript, jqvery, BackBone"

Terms: Scripting client side JavaScript, jqvery, BackBone