We can use a database ORM such as Eloquent to connect your SlimPHP application to a database. We need to setup the databse and Model in best possible way. We are using Lavravel Eloquent ORM which provides a beautiful, simple ActiveRecord implementation for working with your database. We can use Eloquent outside of Lavravel and can easily use in our Slim project. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.
Step 1: Define Database schema
Inside phpmyadmin create a database name slimDB and add new table called users. Add following field ID, Name, Email, Password, Created_at, update_at. Where ID is integer, auto increment and primary field. Define database as utf8_unicode_ci.
Step 2: Install illuminate/database
composer require illuminate/database
Step 3: Configure the databse
In bootstrap/app.php we have set up the database configuration and we need to a way to define eloquent within our Slim project.
Now test database or eloquent in our app/Controllers/homeController.php add this code.