Using Eloquent ORM with Slim

by Shahroze Nawaz  August 8, 2017

Previously, I wrote a very comprehensive tutorial on using twig templating with Slim micro framework. In that tutorial, I created an MVC type architecture to render views from controllers. In this article, I will use the MVC structure of the previous article and connect & use Eloquent ORM with Slim

Eloquent ORM is the default database driver for Laravel and can be used independently with any PHP framework such as Slim. Eloquent ORM throws objects of table data and you need to create class instances to catch them. In this article, I will use ORM in the controller class.

A Recap Of Directory Structure

The project’s directory structure will resemble the structure I created in the previous article. I will use four main folders, app, bootstrap, public, and vendor.

In the app folder, I have the Controller and Models folders, which contain two controllers and model files to render and fetch MySQL data. The bootstrap folder contains app.php which contains the logic of the application. The public folder will run the Slim application publically, and the vendor folder contains the dependencies that are automatically installed with the Slim framework. Following is the complete picture of the structure:

Installing Slim & Eloquent

At this point, I assume that you have already signed up with Cloudways and have launched your PHP application.  Now installing Slim and Eloquent is easy!

To install Slim and Eloquent, move to the project folder and run the following Composer (comes pre-installed on the Cloudways Platform) commands:

For Slim:

For Eloquent:

The next step is to create a database in MySQL.

Create Database and Tables

I will start by creating a simple database with the name “slim” and a table “users”. At this stage, use the following SQL query:

This query will create a simple table with demo records in the database.

Create Eloquent Connection

Now it’s time to create Eloquent connection in app.php file, which is my base file for initiating the application. Simply pass the config setting object to slim instance and pass it to global container after booting the connection. Note that, I have already created app.php in the previous article.

Create Models & Define Fillables

The next step is the creation of a model for running Eloquent queries from HomeController. Here, I will define fillable items which are simply the data that need to be filled and inserted in the database. Fillables are enabled by default in Eloquent, so you must declare them in the model. Create a User model and paste the following code in it:

If you need to understand namespaces, I have already created the namespace in the previous tutorial that you could revisit.

Create Queries in HomeController

The User model should now be ready in the User.php file. Now it’s time to create a query in the index method of HomeController class:

The last thing is to define the route to run the app. Open route.php file and add the following code:

So whenever user hits your_app_url/public/create the above database record will be created.
Now add one more method to get all data and show some properties in browser

Next add the new route for this method in routes.php


 

Following GIF illustrates how the basic app creates and shows the records with Eloquent:

Conclusion

As you could see, using Eloquent with Slim is really easy. In this article I’ve demonstrated how you can configure and use Eloquent with the Slim framework. For reference, you may also want to visit the article I wrote that elaborates the configuration of Eloquent with the Silex framework.

If you have any queries and questions do leave a comment below.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Stay Connected:

You Might Also Like...