Use the database query builder and EloquentORM outside the Laravel framework

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.