Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Use Eloquent Illuminate in PHP Without Laravel

July 20, 2017

3 Min Read
Reading Time: 3 minutes

Ask Laravel enthusiasts and they will tell you that Laravel Eloquent is one of the best parts of the framework. Have you ever thought of using Eloquent in Best PHP Hosting without installing the entire Laravel framework?

php eloquent illuminate

This is what I am going to discuss in this article. I will create a small ACL using Laravel Eloquent. The complete code of this tutorial can be found at this repo.

You might like: How To Host PHP On DigitalOcean

Setup Eloquent in PHP

First, create a new folder in the public directory and name it “eloquent”. Now run the following composer command to install Eloquent.

composer require illuminate/database

Wait for composer to finish the installation.

Create a new file and name it bootstrap.php. I will use this file to setup Eloquent. Now paste the following code in it.


require "vendor/autoload.php";

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;


   "driver" => "mysql",

   "host" =>"",

   "database" => "acl",

   "username" => "root",

   "password" => ""




The code starts with requiring the composer autoload class, then the required Eloquent Manager (Capsule), and finally created its instance. Through Capsule, I have set the database connection as global, and then booted Eloquent. After that, I created a new database on the localhost with the name “acl”.

Create Table For Migration

First create a new folder and name it “database”. Now, create a new file for the users table and name it “User.php”. Next, paste the following code in it:


require "../bootstrap.php";

use Illuminate\Database\Capsule\Manager as Capsule;

Capsule::schema()->create('users', function ($table) {










The above code is similar to what I would write in Laravel. The difference is the `schema()` with reference from `Capsule` class.  I will now create table for the roles. For that, create a new file and name it “Todo.php”. Paste the following code in it:


require "../bootstrap.php";

use Illuminate\Database\Capsule\Manager as Capsule;

Capsule::schema()->create('todos', function ($table) {









I will now migrate all the files. For that open your browser and head to  eloquent/database/user.php and eloquent/database/todo.php. If you see a blank page, the tables have been migrated successfully.

Create Model Class With Eloquent

I will now create model classes using Eloquent.

Create a new folder and name it classes. Now open composer.json file and paste the following lines after require:

"autoload": {

       "classmap": [




This will help me add classes folder as autoload. Now create a new file inside the classes folder and name it User.php.  Paste the following code:


use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Eloquent


   * The attributes that are mass assignable.
   * @var array

   protected $fillable = [

       'name', 'email', 'password','userimage'


   * The attributes that should be hidden for arrays.
   * @var array

   protected $hidden = [

       'password', 'remember_token',


   * Get Todo of User

   public function todo()

       return $this->hasMany('Todo');



Next, create a new file, name it Todo.php and paste the following code:


use Illuminate\Database\Eloquent\Model as Eloquent;

class Todo extends Eloquent


   protected $fillable = ['todo','category','description'];


All the classes are now in place and I could go ahead with the testing.

Test the PHP Code

I will now create a new file at the root and name it index.php.  Open this file and paste the following code in it:


require "bootstrap.php";

$user = User::Create([    'name' => "Ahmed Khan",    'email' => "",    'password' => password_hash("ahmedkhan",PASSWORD_BCRYPT), ]);


   'todo' => "Working with Eloquent Without PHP",

   'category' => "eloquent",

   'description' => "Testing the work using eloquent without laravel"


When you execute this file, you will see the following output:

You might also like: Getting Started With PHPUnit For Unit Testing


In this article, I discussed how you could easily use PHP Eloquent Illuminate without installing Laravel. For a more complete reference on Eloquent, check out the official Laravel documentation. If you need any help with the code, so leave a comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch Laravel apps with 1-Click Install. Painless Server Management

Deploy your apps on optimized Laravel hosting servers.

Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

BFCM 2019