Model – How Does Eloquent Work?

The Eloquent ORM provides an ActiveRecord implementation to work with your database. This means that each model you create in your MVC structure corresponds to a table in your database. Models are mostly used to interact with the database using Eloquent ORM.

Note that we did not tell Eloquent which table to use for our User model. The “snake case”, plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the User model stores records in the users table.
Example A User model will correspond to a users table in your database. Since we have convention when creating our models and database tables, we can easily call data from our database.

For example, to get all Users from a User model, all you would do is User::all(). Using this function would query the database and generate the proper SQL command. Here are some quick examples:

Description                        Function
Find all Users                    User::all()
Find a record                     User::find(id)
Delete a record                 User::delete(id)

Step 1: First we have to create table name users, plural in our database and add required field.

Step 2: Defining Eloquent Model Class
Create a folder called Models inside app folder and inside the Models folder add new file called User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

Class User extends Model {
    protected $table = 'users';

    protected $fillable = [
      'email',
      'name',
      'password',
    ];

}

Note that we did not tell Eloquent which table to use for our User model. The “snake case”, plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the User model stores records in the users table. We can use Model name as directly connection to our database table.

Example A User model will correspond to a users table in your database. Since we have convention when creating our models and database tables, we can easily call data from our database.
In above code first we need to extend our user model with Model class and have to define what field to be specifically we want to write to database table through define table field name in $fillable.

Step 3: Access our our database action in controller through Model name
a. First we have to include our Model class name.
b. We can define a method to access database through model name and methods.

In example below we are inserting a new record our database table User.

<?php

namespace App\Controllers;

use App\Models\User;
use Slim\Views\Twig as View;

class HomeController extends Controller {

 public function index($req, $res){
 User::create([
 'name' => 'Test Allu',
 'email' => 'allutest@gmail.com',
 'password' => 1234,
        ]);
 
 return $this->view->render($res, 'home.twig');
    }
}
You could use several prebuilt Eloquent functions for this purpose.
Retrieving All Records
    $users = User::all();
Retrieving A Record By Primary Key
    $user = User::find(1);
    var_dump($user->name);
Querying Using Eloquent Models
$users = User::where(‘votes’, ‘>’, 100)->take(10)->get();
foreach ($users as $user){
    var_dump($user->name);
}
Eloquent Aggregates
Of course, you may also use the query builder aggregate functions.
$count = User::where(‘votes’, ‘>’, 100)->count();