Support Online
Skip to main content

Creating a Database Model Using Eloquent ORM with Laravel

In this guide, you will learn how to create a database model using Eloquent ORM (Object Relational Mapper) within the Laravel framework. Eloquent allows you to interact with database tables in an object-oriented approach and makes it easy to write SQL queries. This guide walks you through creating a Link model class for table links using Laravel Artisan's make:model command. The goal is to represent database tables as objects and no additional configuration is needed once the model is created.

1. Eloquent ORM and Model Concepts

Eloquent is a powerful ORM tool that comes with Laravel by default and aims to streamline database operations. Some of the advantages that Eloquent provides include:

  • Model and Tables: Each model class represents a single table in your database.
  • Table Name Plural Rule: Eloquent automatically matches the table name by taking the plural form of the model name. For example, the pattern Link matches the chart links.

2. Creating a Model Class with Artisan

In order to operate on database tables with Eloquent ORM, you must first create a model class using Laravel's Artisan CLI tool.

Running the Model Creation Command

You can create a new Link model file using the Artisan command line tool:

docker-compose exec app php artisan make:model Link

This command creates the Link.php file in the app/Models/ directory of the application.

You should see this message in the output of the command:


Model created successfully.

Reviewing the Created File

The generated Link.php file contains a basic model template:


// app/Models/Link.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Link extends Model
{
use HasFactory;
}

This class is derived from Laravel's base Model class and does not require any additional configuration at this time. The model is ready to match the links table according to Eloquent rules.


3. Benefits and Use of the Model

After creating the model, you can start object-based interaction with the database through Eloquent. Thanks to Eloquent, you can perform CRUD (Create, Read, Update, Delete) operations in an object-oriented way instead of writing SQL.

Data Management

You can perform CRUD operations on the database:

Create: The process of adding new data

Reading: Retrieving data from the database

Update: Update existing data

Delete: Deleting data from the database

Relationship Definition

You can make your application broader. For example, you can define a model's relationships with other models. You can use methods such as hasMany, belongsTo for relationships.

Special Methods

You can add custom functions to the model class that contain database queries.


4. Frequently Asked Questions (FAQ)

  1. What is the relationship between Model and Migration?

Migration creates and manages the structure (schema) of the database table. The model allows you to interact with the records (data) in the table through this structure.

  1. What if I don't want to match my model name to the table name?

If the model name does not follow the plural rule or your table name is different, you can specify the table name by adding the $table property inside your model class:


protected $table = 'baglantilar';
  1. What is HasFactory and what does it do?

The HasFactory property adds a Factory to your model. This is used to create fake data, especially during testing and seeding.

  1. What is Mass Assignment and why is it dangerous?

Bulk assignment is to collectively save all data from the user directly into a Model. The danger is that a malicious user can modify columns that should not be allowed (e.g. is_admin). This risk is prevented by using the $fillable or $guarded properties.