Laravel: Wondering how you can use Eloquent ORM outside of Laravel?
Most people would wish to use the power of Eloquent ORM outside of a Laravel project, probably in their pure PHP projects. Well, you should know it is possible.
What is Eloquent?
Eloquent is an ORM. ORM stands for Object-relational mapping. Eloquent is just a nice layer over database queries. Basically it setups the connection for you and makes building queries easier. Other examples of ORM are SQLAlchemy for Python, Hibernate for Java etc
More info and resources in the link below
I will make it easy and bring the information from the link here.
Install
composer require illuminate/database
Illuminate Database
The Illuminate Database component is a full database toolkit for PHP, providing an expressive query builder, ActiveRecord style ORM, and schema builder. It currently supports MySQL, Postgres, SQL Server, and SQLite. It also serves as the database layer of the Laravel PHP framework.
Usage Instructions
First, create a new "Capsule" manager instance. Capsule aims to make configuring the library for usage outside of the Laravel framework as easy as possible.
use Illuminate\Database\Capsule\Manager as Capsule; $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ]); // Set the event dispatcher used by Eloquent models... (optional) use Illuminate\Events\Dispatcher; use Illuminate\Container\Container; $capsule->setEventDispatcher(new Dispatcher(new Container)); // Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent();
composer require "illuminate/events" required when you need to use observers with Eloquent.
Once the Capsule instance has been registered. You may use it like so:
Using The Query Builder
$users = Capsule::table('users')->where('votes', '>', 100)->get();
Other core methods may be accessed directly from the Capsule in the same manner as from the DB facade:
$results = Capsule::select('select * from users where id = ?', [1]);
Using The Schema Builder
Capsule::schema()->create('users', function ($table) { $table->increments('id'); $table->string('email')->unique(); $table->timestamps(); });
Using The Eloquent ORM
class User extends Illuminate\Database\Eloquent\Model {} $users = User::where('votes', '>', 1)->get();
For further documentation on using the various database facilities this library provides, consult the Laravel framework documentation.
Note: This comes fully with models, collections etc. But it does not come with the Paginator objects. For that you need to add it separately. Install as shown below
composer require illuminate/pagination
Here we go!
Analista de Sistemas, Programador full stack. Profesor particular
1 年Excelent tutorial it's Just what i need to my academic project