---
description: https://www.cloudways.com/blog/eloquent-illuminate-in-php-without-laravel/
---

# Use Eloquent Illuminate in PHP Without Laravel P2

## Use Eloquent Illuminate in PHP Without Laravel

![](https://secure.gravatar.com/avatar/b1323fb69ffd7246eb4da6d134d78b5f?s=128\&d=mm\&r=g) [Ahmed Khan](https://www.cloudways.com/blog/author/ahmed-khan/) &#x20;

Updated on June 16, 2021&#x20;

&#x20;3 Min Read

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](https://www.cloudways.com/en/php-cloud-hosting.php) without installing the entire Laravel framework?

![php eloquent illuminate](https://www.cloudways.com/blog/wp-content/uploads/php-eloquent-illuminate.jpg)

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](https://github.com/ahmedkhan847/eloquentwithcustomphp).

**You might like:** [How To Host PHP On DigitalOcean](https://www.cloudways.com/blog/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.

1. 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.

1. \<?php
2.
3.
4.
5. require "vendor/autoload.php";
6.
7. use Illuminate\Database\Capsule\Manager as Capsule;
8.
9.
10.
11. $capsule = new Capsule;
12.
13.
14.
15. $capsule->addConnection(\[
16.
17. "driver" => "mysql",
18.
19. "host" =>"127.0.0.1",
20.
21. "database" => "acl",
22.
23. "username" => "root",
24.
25. "password" => ""
26.
27. ]);
28.
29. $capsule->setAsGlobal();
30.
31. $capsule->bootEloquent();

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:

1. \<?php
2.
3. require "../bootstrap.php";
4.
5.
6.
7. use Illuminate\Database\Capsule\Manager as Capsule;
8.
9.
10.
11. Capsule::schema()->create('users', function ($table) {
12.
13. $table->increments('id');
14.
15. $table->string('name');
16.
17. $table->string('email')->unique();
18.
19. $table->string('password');
20.
21. $table->string('userimage')->nullable();
22.
23. $table->string('api\_key')->nullable()->unique();
24.
25. $table->rememberToken();
26.
27. $table->timestamps();
28.
29. });

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:

1. \<?php
2.
3. require "../bootstrap.php";
4.
5.
6.
7. use Illuminate\Database\Capsule\Manager as Capsule;
8.
9.
10.
11. Capsule::schema()->create('todos', function ($table) {
12.
13. $table->increments('id');
14.
15. $table->string('todo');
16.
17. $table->string('description');
18.
19. $table->string('category');
20.
21. $table->integer('user\_id')->unsigned();
22.
23. $table->timestamps();
24.
25. $table->foreign('user\_id')->references('id')->on('users')->onDelete('cascade');
26.
27. });

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**:

1. "autoload": {
2.
3. "classmap": \[
4.
5. "classes"
6.
7. ]
8.
9. }

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:

1. \<?php
2.
3. use Illuminate\Database\Eloquent\Model as Eloquent;
4.
5. class User extends Eloquent
6.
7. {
8.
9. /\*\*
10. \* The attributes that are mass assignable.
11. \*
12. \* @var array
13. \*/
14.
15. protected $fillable = \[
16.
17. 'name', 'email', 'password','userimage'
18.
19. ];
20.
21. /\*\*
22. \* The attributes that should be hidden for arrays.
23. \*
24. \* @var array
25. \*/
26.
27. protected $hidden = \[
28.
29. 'password', 'remember\_token',
30.
31. ];
32.
33. /\*
34. \* Get Todo of User
35. \*
36. \*/
37.
38. public function todo()
39.
40. {
41. return $this->hasMany('Todo');
42.
43. }
44.
45. }

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

1. \<?php
2.
3.
4.
5. use Illuminate\Database\Eloquent\Model as Eloquent;
6.
7.
8.
9. class Todo extends Eloquent
10.
11. {
12.
13. protected $fillable = \['todo','category','description'];
14.
15. }

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:

1. \<?php
2.
3. require "bootstrap.php";
4.
5. $user = User::Create(\[ 'name' => "Ahmed Khan", 'email' => "ahmed.khan@lbs.com", 'password' => password\_hash("ahmedkhan",PASSWORD\_BCRYPT), ]);
6.
7. print\_r($user->todo()->create(\[
8.
9. 'todo' => "Working with Eloquent Without PHP",
10.
11. 'category' => "eloquent",
12.
13. 'description' => "Testing the work using eloquent without laravel"
14.
15. ]));

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

**You might also like:** [Getting Started With PHPUnit For Unit Testing](https://www.cloudways.com/blog/getting-started-with-unit-testing-php/)

### Conclusion

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.
