Laravel 10 Eloquent Mutators and Accessors

https://coderadvise.com/wp-content/uploads/2023/05/Laravel-10-Eloquent-Mutators-and-Accessors.jpg

Laravel 10 Eloquent Mutators and Accessors

The mutator modifies the value before inserting it into the database, while the accessor modifies the value after fetching it from the database. Mutator and accessor can be defined in the same method.

Let’s suppose, the customer’s name should be stored in lowercase and displayed in Pascal case (with space) even if the customer puts a name with capitalized letters. To do that, we will need to use a mutator and accessor in the Model. The mutator will be converting the customer name into lowercase before inserting it in the database and the accessor will be converting it into Pascal case before displaying it.

You can define a mutator and accessor in the Model file in the app/Models directory. We added two additional columns first_name and last_name into the users table through migration. By default, Laravel creates a name column in the users table.

Mutator

The Mutator allows you to modify the value before inserting it into the database. For example, if you want, the first and last should always be stored in lowercase in the database then mutator is the best way to do it.

Define Mutator:

To define a mutator it is necessary to create a method using the column’s name written in the camel case. Let’s take an example: if the column name is first_name then the method name should be firstName().

In the earlier versions of Laravel, it is required to add a set at the beginning of the method and an attribute keyword at the end of the method. But in Laravel versions 9 and 10 it is no longer required.

Let’s create a mutator for the first_name column in the User model class from app/Models directory.

Before adding the above mutator method in the model class make sure the Attribute class is imported. You can import the attribute class through the below namespace

use Illuminate\Database\Eloquent\Casts\Attribute;

protected function firstName(): Attribute
{
    return Attribute::make(
        set: fn (string $value) => strtolower($value),
    );
}

The above mutator method will be converting the value of the first_name column into lowercase before inserting it into the database.

In this method, we used the make() method of the Attribute class that takes two optional parameters get and set. We passed a callback function to the set parameter that takes a string and converts it into lowercase through PHP built-in function strtolower().

Now look at the below profile update form where the first name is in capitalized letters that should be inserted in the lowercase through the mutator.

Let’s check the inserted row in the database.

In the above screenshot of the users table the first_name value is inserted in lowercase.

Accessor

The accessor allows you to modify the value before being accessed. To define an accessor, similar to a mutator, it is essential to create a method using the column’s name in the camelcase.

Define Accessor:

Let’s create an accessor for the first_name column that should capitalize the first letter of each word.

protected function firstName(): Attribute
{
    return Attribute::make(
        get: fn (string $value) => ucwords($value),
    );
}

In the above accessor method, we used the make() method from the attribute class in which we passed a callback function to the get parameter. The callback function takes the value and converts capitalize the first letter of each word through the PHP built-in function ucwords().

To check if the accessor is working, we can print the first_name column’s value in the controller.

echo $request->user()->first_name;
// Output: Emily

As you can see the first_name value “Emily” is stored in the lowercase through the mutator, but in the controller accessor capitalized the first letter.

Accessor and mutator can be defined in the same method. Look at the below method in which we defined the accessor and mutator together.

protected function firstName(): Attribute
{
    return Attribute::make(
        get: fn (string $value) => ucwords($value),
        set: fn (string $value) => strtolower($value),
    );
}

Related Posts

Laravel News Links