Eloquent, an Object-Relational Mapper (ORM) simplifies database interactions. When using Eloquent, each database table has its own “Model” that is used to communicate with it. Eloquent models allow you to insert, update, remove  and retrieve records from the database table. Before you start, make sure to include a database connection in your application’s config/database.php configuration file.

Generating Model Classes

Let’s start by creating an Eloquent model. Models extend the Illuminate\Database\Eloquent\Model class and are usually located in the app\Models directory. You can use the below artisan command to create a fresh model.

php artisan make:model User

When generating the model, you can use the –migration or -m option to generate a database migration:

php artisan make:model User --migration

You can also create different types of classes, like factories, seeders, controllers, policies, and form requests. In addition, you can combine these options to create numerous classes at once:

 

# Generate a model and a UserFactory class...
php artisan make:model User --factory
php artisan make:model User -f

# Generate a model and a UserSeeder class...
php artisan make:model User --seed
php artisan make:model User -s

# Generate a model and a UserController class...
php artisan make:model User --controller
php artisan make:model User -c

# Generate a model, UserController resource class, and form request classes...
php artisan make:model User --controller --resource --requests
php artisan make:model User -crR

# Generate a model and a UserPolicy class...
php artisan make:model User --policy

# Generate a model and a migration, factory, seeder, and controller...
php artisan make:model User -mfsc

# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...
php artisan make:model User --all
php artisan make:model User -a

# Generate a pivot model...
php artisan make:model Member --pivot
php artisan make:model Member -p

Inspecting Laravel models

Take a look at the model:display Artisan command, which offers a useful summary of every feature and relationship in the model:

php artisan model:show Location

The app/Models directory contains the models created by the make:model command. Let’s look at a simple model class and talk about some of the most important Eloquent conventions:

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class User extends Model
{
    // ...
}

Table Names

You may have noticed that we did not provide to Eloquent which database table matches our User model after taking a quick look at the sample above. Unless another name is specifically stated, the “snake case” plural name of the class will be used as the table name by convention. Thus, Eloquent will presume that in this instance, records are stored in the users table by the User model.

You can manually define the model’s table name by specifying a table property on the model, in the event that the associated database table for your model does not follow this convention:

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class Users extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'sw_employees';
}

Primary Keys

Eloquent assumes that each model’s related database table has an id primary key column. If necessary, you can define a protected $primaryKey property on your model to select an alternate column to serve as the primary key.

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class User extends Model
{
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'sw_emp_id';
}

 

For more information, check out Laravel Eloquent: Getting Started documentation.

Leave A Comment