The database query builder and EloquentORM used outside the Laravel framework have a Capsule directory in the illuminate/database package of the Laravel framework core code, which has a Manager. php file. if you want to use the Illuminate Database component outside Laravel, you must use this file. Taking Yii2 as an example, we first run the following Composer command in the project root directory to install the dependency package:
composer require illuminate/database ~5.1
In this way, the illuminate/database package is now available under the vendor Directory. next we will modify the entry file index. php as follows:
Run ();
Then, create system/eloquent/Start. php under the Project root directory and edit the file as follows:
'Mysql', 'host' => DB_HOST, 'database' => DB_NAME, 'username' => DB_USER, 'password' => DB_PASSWORD, 'charset' => 'utf8', 'colation' => 'utf8 _ unicode_ci ', 'prefix' => DB_TABLEPREFIX,]; use Illuminate \ Container; use Illuminate \ Database \ Capsule \ Manager as Capsule; $ capsule = new Capsule; // create a link $ capsule-> addConnection ($ database ); // Set Global Static access $ capsule-> setAsGlobal (); // start Eloquent $ capsule-> bootEloquent ();
Finally, we can create a model class in the models directory as follows:
In this way, we can use the User model in the code with the syntax of the Eloquent model class. For how to use Eloquent ORM, refer to the Eloquent ORM document.