Author: Shiannizhilu public number: Zhixing Research Institute

8.1 Accessors for the model

What is an accessor?

The accessor intercepts and modifies the property when it gets the list of data.

Accessors are methods that are written in the model and are named in a fixed format:

Accessor name: fixed before get, fixed after Attribute, add field name in the middle.

For example, if the field name is gender, the accessor is getGenderAttribute.

For example, if the field name is user_name, the accessor is getUserNameAttribute.

Here are a few examples:

For example, if the gender is printed in parentheses, we can add the following method in model user.php:

// The $value parameter is the value of the source field and can be modified
public function getGenderAttribute($value)
{
    return '【'.$value.'】';
}
Copy the code

Execution result:

2. For example, if the state in the table is a number, the number refers to a state, then the accessor can be written to the controller:

public function getStatusAttribute($value)
{
    $retStatus= [-3= >'Deleted',
        -2= >'Screened',
        -1= >'Screened'.0= >'normal'.1= >'VIP'.2= >'SVIP',];return $retStatus[$value];
}
Copy the code

Execution result:

3, we can also create a virtual field, using the existing data field consolidation, but the data should be appended.

First append virtual fields to the model:

// Append virtual fields to the list of data objects
protected $appends = ['info'];
Copy the code

Then create the accessor for the virtual field:

// Create a virtual field
public function getInfoAttribute()
{
    return $this->username.The '-'.$this->gender;
}
Copy the code

If executed directly, the output looks like this:

This is because we changed the accessor for the gender field earlier, so we get the same result as before.

If gender has previously been modified by an accessor, the above method will result in a modified gender;

If you want to use the source field to make changes, you need to use the following format:

// Create a virtual field
public function getInfoAttribute()
{
    return $this->attributes['username'].The '-'.$this->attributes['gender'];
}
Copy the code

The output of the new info field is: crayon new – male.

8.2 Model modifiers

Modifiers, as opposed to accessors, intercept as they write to the database, make changes, and then write.

The modifier is named in the same way as the accessor, except that it changes get to set.

Here are some examples of what this might do:

1, case conversion:

// modifiers that convert the mailbox to uppercase when writing data
public function setEmailAttribute($value)
{
    $this->attributes['email'] = strtoupper($value);
}
Copy the code

2. Add default date columns, default created_at and updated_at;

// Set the column where the date can be automatically written
protected $dates = [
    'details'
];
Copy the code

3, you can set the type of the output field, such as a Boolean type, output is true and false;

// Set the field type
protected $casts = [
    'details'= >'boolean'
];
Copy the code

The above.