Guide to Creating a Database Model with Laravel Eloquent ORM
💡 What You Will Learn in This Guide
In this guide, you will learn how to create model classes that represent database tables using the Laravel Eloquent ORM structure. Thanks to Eloquent, you can manage database records with an object-oriented (OOP) approach without writing SQL queries.
🧠 Technical Summary
- Subject: Creating a database model structure with Eloquent ORM.
- Purpose: To manage table operations through models without writing SQL queries manually.
- Tools: Laravel Artisan CLI, Eloquent ORM.
🏗️ Step 1: Eloquent Model Concept
Eloquent serves as Laravel's object relational mapper (ORM). Each model represents a table in the database.
-
Model names are singular, table names are plural.
Example: Model
Linkis linked to tablelinksby default. -
add, delete, update, query operations can be performed through the model.
⚙️ Step 2: Creating a New Model
You can create a new model with the following Artisan command:
php artisan make:model Link
This command creates the file
app/Models/Link.php.
Created class:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Link extends Model
{
use HasFactory;
}
This class inherits Eloquent's
Modelclass and contains all ORM features.
🧩 Step 3: Customizing the Model
You can customize the model in the future. For example:
// Tablonun adını değiştirmek
protected $table = 'baglantilar';
// Otomatik timestamp alanlarını kapatmak
public $timestamps = false;
// Toplu atama yapılabilir alanlar
protected $fillable = ['url', 'description', 'enabled'];
$fillableis important for secure data assignments.
🔗 Step 4: Model Relationships
Relationships between tables can be easily established with Eloquent:
public function user()
{
return $this->belongsTo(User::class);
}
This relationship specifies that each
Linkrecord belongs to aUsermodel.
Other types of relationships:
hasOne– one to onehasMany– one to manybelongsToMany– many to many
⚡ Step 5: Co-creating the Model with Migration
You can create the model and migration file at the same time:
php artisan make:model Proje -m
This command produces both the model
Projeand the migration file namedcreate_projes_table.
❓ Frequently Asked Questions (FAQ)
1. What is ORM and why should I use it?
ORM maps database tables to programming language objects. It allows you to perform object-based data operations without writing SQL.
2. Why should I define $fillable or $guarded in my model file?
Provides security in bulk data assignments. $fillable, which columns data can be written to; $guarded indicates which ones to preserve.
3. Can the model be in a different folder?
Yes. You can use a different directory, but you must update the namespace definition (namespace App\Models;) accordingly.
4. Is it mandatory to define a relationship?
No, but it is recommended for managing cross-table relationships in large projects.
5. How can I use the Eloquent model in tests?
Thanks to the HasFactory feature, you can quickly create test data.
🏁 Result
You can now create and manage your database models with Laravel's powerful ORM structure Eloquent. You can write safe and readable codes with an object-oriented structure, without the need for SQL queries.
Try Eloquent models now by starting your own Laravel project on the GenixNode platform! 🚀

