Skip to content
Tutorial emka
Menu
  • Home
  • Debian Linux
  • Ubuntu Linux
  • Red Hat Linux
Menu

Cara Menggunakan ScopedBy dan ObservedBy Attributes pada Model Eloquent Laravel

Posted on February 20, 2024

Laravel 10.44 menawarkan sebuah fitur baru untuk model Eloquent ORM. Beberapa developer menambahkan beberapa fitur yang menarik disana.

Misalnya, Eliezer Margareten telah menyumbangkan atribut ScopedBy untuk mendaftarkan global scopes pada model Eloquent. Contohnya:

use AppModelsScopesAncientScope;
use IlluminateDatabaseEloquentAttributesScopedBy;

#[ScopedBy([AncientScope::class])]
class User extends Model
{
    //
}

Kita juga dapat terus menggunakan metode booted() dalam model untuk mendaftarkan global scopes:

/**
 * Metode "booted" dari model.
 */
protected static function booted(): void
{
    static::addGlobalScope(new AncientScope);
}

Atribut ScopedBy menerima observer tunggal atau array observer, dan Anda juga dapat menentukan beberapa atribut untuk model Anda:

// Tipe array
#[ScopedBy([ScopeOne::class, ScopeTwo::class])]

// Tipe string dapat diulang
#[ScopedBy(ScopeOne::class)]
#[ScopedBy(ScopeTwo::class)]

Selain itu ada juga atribut ObservedBy untuk mendaftarkan observer model pada model Eloquent kita:

use AppObserversUserObserver;
use IlluminateDatabaseEloquentAttributesObservedBy;

#[ObservedBy([UserObserver::class])]
class User extends Authenticatable
{
    //
}

Atribut ObservedBy menerima observer tunggal atau array observer, dan Anda juga dapat menentukan beberapa atribut untuk model Anda jika itu gaya kita:

// Array dari kelas
#[ObservedBy([UserObserver::class, AnotherObserver::class])]

// Dapat diulang sebagai string
#[ObservedBy(UserObserver::class)]
#[ObservedBy(AnotherObserver::class)]

Atribut ObservedBy adalah cara tambahan untuk mendaftarkan observer, yang biasanya dilakukan dalam service provider:

public function boot(): void
{
    User::observe(UserObserver::class);
}

Dengan menggunakan ScopedBy dan ObservedBy, kita dapat dengan mudah menerapkan global scopes dan observers pada model Eloquent Laravel kita.

Recent Posts

  • How to Disable AI Features in Firefox 148
  • Git 2.53: What’s New?
  • Linux From Scratch Ditches Old System V init
  • How to Maintained the SSD with TRIM
  • What is CVE-2024-21009? Microsoft Office Security Serious Bug
  • Windows 11 Shutdown Problems: Why Your PC Won’t Turn Off (and What Microsoft’s Doing)
  • What is the Steam Overlay Error?
  • Why Your Computer Thinks Winaero Tweaker is Bad (and Why It’s Probably Wrong!)
  • What is Origami Linux? A Super-Safe, Unchangeable Computer System!
  • Why Does OneNote Freeze? Easy Fixes for Typing & Drawing Problems!
  • What is Protected File System (PFS) in Windows 11?
  • Linux News Roundup February 2026
  • How to Install JellyFin Media Server on Samsung TV with TizenOS
  • Why OneNote Clears Your Notes
  • AMD NPU Monitoring on Linux: A Beginner’s Guide to AI Chip Tracking!
  • How to Fix AMD Adrenalin’s Game Detection Issues on Windows
  • Greg Kroah-Hartman Wins Multiple Award at European Open Source Awards!
  • What are Microsoft Copilot Reminders?
  • What’s New in Plasma 6.7? Quick Notification History Clear-Up!
  • Awesome Alternatives to Microsoft Defender Application Guard (MDAG)
  • How tto Enable DLSS 4 & 5 for Your GPU
  • Backlinks: Why They’re Super Important for Your Website!
  • Gnome’s Smart Windows: Tiling Shell 17.3 Makes Organizing Your Screen Easier!
  • How to Replace Notepad with Microsoft Edit
  • What’s New in Shotcut 26.1?
RSS Error: https://emka.web.id/feed is invalid XML, likely due to invalid characters. XML error: Invalid character at line 1481, column 37
  • Cara Membuat Podcast dari PDF dengan NotebookLlama dan Groq
  • Tutorial Membuat Sistem Automatic Content Recognition (ACR) untuk Deteksi Logo
  • Apa itu Google Code Wiki?
  • Cara Membuat Agen AI Otomatis untuk Laporan ESG dengan Python dan LangChain
  • Cara Membuat Pipeline RAG dengan Framework AutoRAG
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025
©2026 Tutorial emka | Design: Newspaperly WordPress Theme