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

  • Is it Time to Replace Nano? Discover Fresh, the Terminal Text Editor You Actually Want to Use
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to Clear Copilot Memory in Windows 11 Step by Step
  • How to Show Battery Percentage on Windows 11
  • How to Fix VMSp Service Failed to Start on Windows 10/11
  • How to Fix Taskbar Icon Order in Windows 11/10
  • How to Disable Personalized Ads in Copilot on Windows 11
  • What is the Microsoft Teams Error “We Couldn’t Connect the Call” Error?
  • Why Does the VirtualBox System Service Terminate Unexpectedly? Here is the Full Definition
  • Why is Your Laptop Touchpad Overheating? Here are the Causes and Fixes
  • How to Disable All AI Features in Chrome Using Windows 11 Registry
  • How to Avoid Problematic Windows Updates: A Guide to System Stability
  • What is Microsoft Visual C++ Redistributable and How to Fix Common Errors?
  • What is the 99% Deletion Bug? Understanding and Fixing Windows 11 File Errors
  • How to Add a Password to WhatsApp for Extra Security
  • How to Recover Lost Windows Passwords with a Decryptor Tool
  • How to Fix Python Not Working in VS Code Terminal: A Troubleshooting Guide
  • Game File Verification Stuck at 0% or 99%: What is it and How to Fix the Progress Bar?
  • Why Does PowerPoint Underline Hyperlinks? Here is How to Remove Them
  • AI Bug Hunting with Semgrep
  • What is the Excel Power Query 0xc000026f Error?
  • How to Build Your Own Homelab AI Supercomputer 2026
  • Tutorial SEO Anchor Link: Cara Dapat Ranking di Google Lewat Strategi Link Building yang Aman
  • Inilah Huawei Pura 90 Pro, HP Flagship dengan Desain Kamera Segitiga Unik dan Performa Kirin yang Makin Gahar
  • Inilah 5 Rekomendasi Kipas Tangan Portable Cas Tahan Lama Biar Kalian Tetap Adem Saat Cuaca Panas Ekstrem
  • Inilah MacBook Neo, Laptop Termurah Apple yang Ternyata Punya Performa Gaming Gila!
  • Inilah Daftar Tablet SIM Card 5G dengan Keyboard Terbaik 2026 untuk Kerja Remote
  • Complete Tutorial on OSMnx: Turning Map Data into Powerful AI World Models
  • Complete Tutorial: Mastering AI Video Generation with Seedance 2.0 and Higgsfield
  • How to Create Blockbuster-Style Cinematic AI Videos: A Complete Tutorial for Beginners Using Visiana AI
  • How to Master Next-Gen AI Training: A Beginner Tutorial on In-Place TTT and Domain-Invariant Neurons for Smarter Models
  • Complete Tutorial: Integrating Open Claw with Discord, GitHub, and Google for Personal AI Productivity
  • 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