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 Generate Battery Report in Windows 11: A Simple Guide
  • How to Setting Up a Pro-Level Security System with Reolink and Frigate NVR
  • How to Install DaVinci Resolve on Nobara Linux and Fix Video Compatibility Issues Like a Pro
  • How to Master GitHub’s New Power Tools: Copilot CLI, Dashboards, and More!
  • How to Create and Configure DNS Server on RHEL 10
  • How a Security Professional Bypassed a High-Security Building Using Just a Smartphone and a QR Code
  • A Step-by-Step Guide to Upgrading Uptime Kuma to Version 2.0
  •  How to Disable Bing Search in the Windows 11 Start Menu for Better Privacy
  • How to Transitioning from Engineer to Product Manager
  • How to Managing GitHub Pull Requests Directly from Your Terminal with Github-CLI
  • How to Building a Privacy-First Self-Hosted App Stack, 2026!
  • How to Remove Underlines from Links in Outlook
  •  How to Fix Microsoft 365 Deployment Tool Not Working: A Complete Troubleshooting Guide
  •  How to Fix Windows 11 ISO Download Blocked and Error Messages
  • How to Make Your Website Vibrate with Web Haptics
  • Measuring LLM Bullshit Benchmark
  • A Step-by-Step Guide to ZITADEL Identity Infrastructure
  • How NVIDIA G-SYNC Pulsar is Finally Fixing Motion Blur Forever
  • How Multipathing Keeps Your Linux Systems Running Smoothly!
  • Forgejo: A Self-hosted Github Alternative You Should Try
  • Introducing Zo Computer, How it Will Changing Personal Data Science Forever
  • Which AI Brain Should Your Coding Agent Use? A Deep Dive into the OpenHands Index
  • Hoppscotch, The Postman Killer: Why You Should Switch from Postman to Hoppscotch Right Now
  • Nitrux 6.0 Released with Linux Kernel 6.19: What’s New?
  • How to Upgrade Pop!_OS 22.04 LTS to 24.04 LTS: A Step-by-Step Guide
  • Inilah Alasan Kenapa Sumbangan ke Tempat Ibadah Biasa Nggak Bisa Langsung Jadi Pemotong Pajak!
  • Belum Tahu? Inilah Cara Pasang Iklan Meta Ads untuk Sales WiFi Supaya Banjir Closingan!
  • Inilah Alur Pengerjaan EMIS GTK 2026 yang Benar dari Awal Sampai Akhir
  • Inilah 27 Sekolah Kedinasan untuk Lulusan SMK 2026, Bisa Kuliah Gratis dan Langsung Jadi CPNS!
  • Inilah Cara Kuliah S2 di Inggris dengan GREAT Scholarship 2026: Syarat Lengkap, Daftar Kampus, dan Tips Jitu Biar Lolos!
  • How to Build Your First AI App with Lovable AI Today!
  • OpenClaw Tutorial: A Step-by-Step Guide to Coding Your Very First Website from Scratch!
  • Seedance 2.0 Is Here! Unlimited + Completely Uncensored AI Video Gen
  • A Step-by-Step Guide to the Qwen 3.5 Small Model Series
  • What new in Google’s Workspace CLI?
  • 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