These Eloquent features move repeated logic into the model, so controllers stay short.
Local scopes
A method named scopePublished($query) becomes Post::published(). Scopes can be chained: Post::published()->latest()->get().
Accessors and mutators
Define a method returning Attribute::make(get: ..., set: ...):
- get changes a value when you read it (e.g. a formatted name)
- set changes a value before it is saved (e.g. lowercase email)
Casts
Casts convert columns automatically:
'is_published' => 'boolean'—0/1becomestrue/false'published_at' => 'datetime'— a Carbon date you can format'settings' => 'array'— JSON column becomes a PHP array'password' => 'hashed'— hashes the value when saving
In Laravel 11 and later casts go in a casts() method; older versions use a $casts property.
Soft deletes
Add $table->softDeletes() to the migration and the SoftDeletes trait to the model. Now delete() only sets deleted_at; the row is hidden from queries but can be brought back with restore(). Use withTrashed() to include deleted rows.
Example
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class Post extends Model
{
use SoftDeletes;
protected function casts(): array
{
return ['is_published' => 'boolean', 'published_at' => 'datetime'];
}
public function scopePublished($query)
{
return $query->where('is_published', true);
}
protected function title(): Attribute
{
return Attribute::make(set: fn (string $value) => Str::title($value));
}
}
// Usage
$posts = Post::published()->latest()->get();
echo $posts->first()?->published_at?->format('d M Y'); // 01 Dec 2026
Saving the title "learn laravel scopes" stores "Learn Laravel Scopes". published_at is a Carbon date, so ->format() works directly.