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

  • Microsoft Edge Replaces Read Aloud with Copilot Vision: What You Need to Know?
  • Microsoft Officially Removes Optional .NET Framework 3.5 in Windows 11
  • Windows 11 Shared Audio Now Available on More Devices
  • How ML Could Improve Linux Kernel Performance
  • Why Do Linux Users Use Non-Free Software? Explained Simply
  • How to Recover Deleted Files from VMware Datastore
  • How to Fix Missing Audio Ports in Device Manager
  • Plex vs Jellyfin: Why Plex is Still the Best Self-Hosted Media Server for Most Users
  • KDE Plasma 6.7 Brings AI-Powered Multitasking to Linux with Smart Window List
  • How to Fix Microsoft 365 License Errors 29 & 44 with Simple Steps
  • KDE Linux Nears Beta with 62% Progress, What’s Next?
  • PeaZip 10.9 Released, This is The New Feature
  • Wine 11.2 Released with Faster Debugging and 32-Bit Fixes for Linux Users
  • Microsoft Release Litebox: A Secure Library OS for Developers
  • Ubuntu 26.04 LTS Explained: New Features & Benefits for Users
  • NVIDIA Blames January 2024 Windows Update for Gaming Performance Issues and Stuttering
  • Microsoft Defender XDR Now Automatically Filters Low-Severity Alerts
  • How to Access OneDrive Backup Files Easily
  • How to Update LibreOffice on Linux: A Simple Guide
  • What’s New on Ardour 9.0? A Major Update for Linux Audio Production
  • Dozzel: The Best Real-Time Docker Log Viewer
  • Calibre 9.2 Released: New ZIP Output and Features for E-Book Lovers
  • Raspberry Pi 4 Rev 1.5 Dual RAM Explained for Beginners
  • Darktable 5.4.1 Released: Major Bug Fixes and New Features
  • GNU Linux vs Just Linux: What’s the Difference Explained
  • Apa itu Desil 1 sampai 10 Di DTSEN Kemensos? Ini Cara Hitungnya
  • Apakah Pinjol Kilat Cicil: Legal atau Ilegal? Penipuan atau Tidak?
  • Cara Bikin Aplikasi SaaS Fullstack Sederhana dalam 10 Menit dengan Claude
  • Berapa Lama Verifikasi Dana Premium? Jangan Panik, Ini Penjelasannya!
  • NotebookLM Sekarang Bisa Kustomisasi Slide Presentasi Secara Dinamis
  • Inilah Cara Bikin Postingan Viral Menggunakan AI
  • Inilah Cara Buat Conversation Starter di Claude Project Agar Workflow Kalian Lebih Sat-Set
  • Cara Membuat Knowledge Base Audit untuk Claude Project Agar Dokumen Kalian Nggak Berantakan
  • Cara Ubah Role Definition Menjadi Custom Instructions yang Efektif buat Claude Project
  • Cara Mendefinisikan Role Project Claude Agar Hasilnya Lebih Akurat dan Konsisten
  • 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