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 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
  • KDE Plasma 6.6.2 Released: Key Bug Fixes and Enhancements Explained
  • Meet the Huawei NetEngine 8000: The Router Powering the Next Generation of AI-Driven Networks!
  • LLM Settings That Every AI Developer Must Know
  • Is Your Second Monitor a Mess? Kubuntu 26.04 Resolute Raccoon Finally Fixes Multi-Display Woes!
  • How to Run Massive AI Models on Your Mac: Unlocking Your Hidden VRAM Secrets
  • How to Create Gemini CLI Agent Skills
  • WTF? Ubuntu Planning Mandatory Age Verification
  • Why This Retro PC is Actually a Modern Beast: Maingear Retro98
  •  Windows 11 Taskbar Update: How to Move and Resize Your Taskbar Again
  • Does KDE Plasma Require Systemd? Debunking the Mandatory Dependency Myths
  •  How to Fix ‘docs.google.com Refused to Connect’ Error in Windows 10/11
  • Aerynos Feb 2026 Update: Faster Desktops and Moss Performance Boost
  • Cara Dapat Diamond Free Fire Gratis 2026, Pemain FF Harus Tahu!
  • Inilah Cara Mengisi Presensi EMIS GTK IMP 2026 Terbaru Biar Tunjangan Lancar
  • Inilah Trik Hashtag Viral Supaya Video Shorts Kalian Nggak Sepi Penonton Lagi
  • Inilah Jawabannya, Apakah Zakat Fitrah Kalian Bisa Mengurangi Pajak Penghasilan?
  • Inilah Caranya Supaya Komisi TikTok dan Shopee Affiliate Tetap Stabil Pasca Ramadhan!
  • How to Automate Your Business Intelligence with Google Antigravity and NotebookLM
  • The Secret Reason Seedance 2.0 is Realistic
  • Exploring Microsoft Phi-4 Reasoning Vision 15B
  • Gemini 3.1 Flash-Lite Released: How to Master Google’s Fastest AI Model for Real-World Projects
  • Qwen Is Ruined! Why the Masterminds Behind Qwen 3.5 Left Alibaba Cloud
  • 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