Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Laravel

Models in Laravel (Eloquent)

Create Eloquent models that map to database tables, set fillable fields, and define relationships.

Models in Laravel (Eloquent) — a practical guide to Laravel Eloquent models with clear examples you can reuse in real projects.

Laravel Tutorial Series (9/27). Prefer one page? Read the complete Laravel tutorial (all 27 topics).

Short description

An Eloquent model represents a database table. (Often written as “Modal” by mistake — in Laravel this is the **Model**.)

Create model + migration

php artisan make:model Post -m

Basic model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Post extends Model
{
    protected $fillable = [
        'user_id',
        'title',
        'slug',
        'body',
        'is_published',
    ];

    protected $casts = [
        'is_published' => 'boolean',
        'published_at' => 'datetime',
    ];
}

Create a record

Post::create([
    'user_id' => auth()->id(),
    'title'   => 'Hello Laravel',
    'slug'    => 'hello-laravel',
    'body'    => 'My first post',
]);

Find and update

$post = Post::findOrFail(1);
$post->update(['title' => 'Updated title']);

Leave a reply

Your email address will not be published. Required fields are marked *