Laravel中Accessor与Mutator是什么?方法命名中间部分解析
Hey there! Let's break down everything you need to know about Laravel's Accessors and Mutators, including that naming rule you're curious about in the examples.
What Are Accessors & Mutators?
These are special methods in Eloquent models that let you customize how attribute values are retrieved (Accessors) and stored (Mutators) in your database. They eliminate repetitive formatting code and keep your data consistent.
Accessor (访问器)
- Definition: A method that automatically formats an attribute's value when you retrieve it from the model.
- Purpose: Standardize how data is displayed to users or other parts of your application, without manually formatting values every time.
- Common Use Cases:
- Capitalizing the first letter of names (like your example)
- Formatting date fields into human-readable strings (e.g., converting
created_attoJanuary 1, 2024) - Generating composite attributes (e.g., combining
first_nameandlast_nameinto afull_namefield)
Example Code:
public function getFirstNameAttribute($value) { return ucfirst($value); }
When you call $user->first_name or $user->firstName, this method runs automatically and returns the capitalized version of the stored value.
Mutator (修改器)
- Definition: A method that automatically formats an attribute's value before it's saved to the database.
- Purpose: Ensure data stored in your database follows a consistent format, regardless of user input or how values are assigned in code.
- Common Use Cases:
- Normalizing strings to lowercase (like your example)
- Encrypting sensitive data before storage
- Converting user-input dates into Carbon objects for consistent database storage
Example Code:
public function setFirstNameAttribute($value) { $this->attributes['first_name'] = strtolower($value); }
When you assign a value like $user->first_name = "JOHN" or $user->firstName = "JOHN", this method converts the input to lowercase before saving it to the first_name database column.
Understanding the FirstName Naming Rule
The middle part of the method name (like FirstName) follows Laravel's automatic camelCase to snake_case mapping rule. Here's how it works:
- Fixed Prefix/Suffix: Accessors start with
get, Mutators start withset, and both end withAttribute. - CamelCase to Snake_Case Conversion: The middle segment (e.g.,
FirstName) is written in camelCase, and Laravel automatically converts it to snake_case to match your database column name.
Let's look at more examples to make this clear:
getLastNameAttribute()→ maps to thelast_namedatabase columnsetEmailVerifiedAtAttribute()→ maps to theemail_verified_atdatabase columngetFullNameAttribute()→ even works for dynamic composite attributes (you don't need afull_namecolumn in your database; this generates the value on the fly when you call$user->full_name)
This rule lets you use PHP-friendly camelCase in your code while working with database columns that typically use snake_case—Laravel handles the translation automatically.
内容的提问来源于stack exchange,提问作者user9551950

