Eloque: Generating Model Classes is a process of creating model classes with the database table in Laravel. Using Eloquent orm (Object-Relational Mapper) you can easily easily read, create, update and delete data from the database table.
Model Class Method
1. Create Model using Artisan Command
Model classes are created using Laravel's Artisan Cli. Run the command below:
php Artisan Make: Model Modelname
php artisan make:model ModelName
for example, to create a post called Post:
php Artisan Make: Model Post
php artisan make:model ModelName
It will create a post.php file in app/models directory.
Basic structure of the model
Model will usually look like this:
Namespace App \ models;
Use illuminate \ database \ eloquent \ factories \ Hasfactory;
Use illuminate \ database \ eloquent \ model;
Class post extends model
{
Use Hasfactory;
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
}
2. Model with migration files
Use the command below to create the model and database migration together:
php artisan make: model post -m
php artisan make:model ModelName
It will create two things:
Post Model. -
Database/Migrations/2025_01_05_000000_Create_Posts_table.php a migration file called -
1. Created Factory and Seeder with models
to create models with Factory and Seeder files:
php Artisan Make: Model Post -MFSC
php artisan make:model ModelName
- m : will create migration.
- f : will create factory.
- S : Cedar will create.
- c : will create controller.
Relationships to the database table in the model
1. Default table name
Laravel Model captures the name of the database table by the name of the class.
Eg:
Posts of Post Model will be posts. -
The default table of the user model will users. -
If you want to use a custom table, set $ Table properties on the model:
Class post extends model
{
Protected $ Table = 'blog_posts';
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
}
Model's Important Properties and Method
1. $ Fillable and $ Guarded
$ Fillable or $ Guarded is used to determine what field can be inserted into the database.
- $ Fillable : Data can be inserted in the fields prescribed.
Class post extends model
{
Protected $ Fillable = ['Title', 'Content'];
}
php artisan make:model ModelName
- $ Guarded : Data cannot be inserted in the fields prescribed.
Class post extends model
{
Protected $ Guarded = ['id'];
}
php artisan make:model ModelName
2. $ Primarykey
If something other than the primary key id on your table is:
Class post extends model
{
Protected $ primarykey = 'post_id';
}
php artisan make:model ModelName
1. $ Timestamps
Laravel uses the table to create_at and updated_at column as default. If you don't want to use them:
Class post extends model
{
Public $ timestamps = False;
}
php artisan make:model ModelName
1. Relationships
Models can be linked to each other in using eloquent relationships.
- One to one relationship (one-to-one) :
Public Function User ()
{
Return $ this-> Hasone (User :: Class);
}
php artisan make:model ModelName
- to many relationships (one-to-many) :
Public Function Comments ()
{
Return $ this-> Hasmany (Comment :: Class);
}
php artisan make:model ModelName
- many to many relationships (many-to-many) :
Public Function Tags ()
{
Return $ this-> belongstomany (tag :: class);
}
public function tags()
{
return $this->belongsToMany(Tag::class);
}