Home Eloquent ORM How to use Eloquent ORM without Laravel Framework

How to use Eloquent ORM without Laravel Framework


Setup Eloquent ORM with Core PHP Application

Laravel framework became a number one framework in php development it has lot of advantages.

  • Easy to run with homestead.
  • Security
  • Flexibility,
  • documentation
  • Real time support

Apart from the above features Laravel comes up with Eloquent ORM Powerful Database component, it  is used to handle all database related operations.

If you already using laravel framework with your project then it’s good you can skip this post, but if you have a existing application, which is already in development phase and you won’t be able to convert it to laravel because of time limitations and still you wish to use Eloquent ORM  for your future task from the project then this post is going to help you to install eloquent and use.

Before following this tutorial you needs to have composer installed on your system which is going to help to pull in eloquent package.

Step 1: Installing Illuminate Database Component

Create project directory called demo,  if you are installing in existing application then just cd in to you project directory with the terminal.

Pull Illuminate Database component using composer:

It will create composer.json file (if not exist) in your root directory with require dependancy, it is going to create vendor folder with the required files and folders in it, don’t worry about it for now.

Make sure you get above message.

if you open composer.json file it would look like this, version number may change according to the availability.

Step 2: Setup autoload using classmap:

Setting up the autoload is important to have our namespace working, we are going to use here classmap, you can also you psr-4 which is also good option.

Open up the composer.json file and following additional lines next to require:

You might thinking, what we are doing by adding autoload and classmap to the json file?

We basically referencing the Model directory to autoload, after adding those lines use following command to dump and autoload your composer.

Step 3: Database connection with Eloquent ORM:

Eloquent ORM currently supports MySQL, Postgres, SQL Server, and SQLite, we can easily setup connection with any of those, I am going to use MySQL here for the demo.

Step 4: Creating Eloquent ORM Model:

Let’s create our first Eloquent Model, create new directory called Models, here we will store our ORM models.

Add new  Task.php file within the Models folder and use following code:

We can use all available options ‘$table’, ‘$primaryKey’ or mass assignment array called $fillable and many more.

Before going to use this model, we need database table, to do than we will use Capsule::schema().

Create new file called createTaskTable.php use following code to create new schema for the table:

It is exactly like database migration in Laravel, try to run this file using terminal or with browser:

To run using terminal type in following command:

It should show success message.

Step 5: Using Eloquent ORM Model:

Before going to use our Task Model we have to update it for mass assignment variable, let’s do that now open the file and update it as showing below:

Add testModel.php file to access our Task model and do some operations:

Use following command to run file:

it should show following output:

If you see new record is created and listed that means your good to go and use Laravel Eloquent ORM  in your project.

Folder Structure:

demo project folder structure

Enjoy this tutorial? Get iTech Empires updates delivered straight to your inbox.

No Spam, ever. We'll never share your email address and you can opt out at any time.

Yogesh Kolihttp://www.itechempires.com
Yogesh Koli is a software engineer & Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 6+ years of experience working with the front-end and back-end web application development.


1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Mariya Shmakova Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of
Mariya Shmakova
Mariya Shmakova

Tnanks for lesson! How can i use Capsule ORM with cache (memcached)?

Tutorials You May Like!

- Advertisement -