😇Laravel 9 Eloquent Mutators and Accessors Example (ok)

https://www.itsolutionstuff.com/post/laravel-9-eloquent-mutators-and-accessors-exampleexample.html

C:\xampp8\htdocs\plugindev\database\migrations\2014_10_12_000000_create_users_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::create('users', function (Blueprint $table) {
      $table->id();
      $table->string('name');
      $table->string('email')->unique();
      $table->timestamp('email_verified_at')->nullable();
      $table->string('password');
      $table->date('date_of_birth');
      $table->rememberToken();
      $table->timestamps();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::dropIfExists('users');
  }
};

C:\xampp8\htdocs\plugindev\app\Models\User.php

C:\xampp8\htdocs\plugindev\app\Http\Controllers\UserController.php

C:\xampp8\htdocs\plugindev\routes\web.php

Hi Dev,

This post will give you examples of laravel 9 mutator and accessor examples. Here you will learn laravel 9 accessors and mutators examples. you will learn mutators and accessors in laravel 9. you will learn what is mutator in laravel 9. you will do the following things for laravel 9 eloquent getter and setter example.

Laravel provides Accessors and Mutators in Eloquent. First of all, I will give you a simple definition of what is Accessors and Mutators in Laravel Eloquent, Then I will give you a very simple example where you will understand what it works.

"Accessors create a custom attribute on the object which you can access as if it were a database column."

"Mutator is a way to change data when it is going set into database table."

Now, I will explain to you by a simple example. if we have a field called "date of birth" with date datatype in the user model. You will give the user to input the date of birth in "m/d/Y" format, but the database accepts "Y-m-d" then you always change the date format before inserting the record into a database. The same thing when you retrieve records from the database then you have show date "m/d/Y" to the user. so it might be multiple places you have to create records and show data, then you have to do the same thing again and again. laravel provides a solution for that to automatically change the value before insert and before getting data using Accessors and Mutators.

So, let's see bellow example and you will got it how it works.

Step 1: Install Laravel 9

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

Step 2: Update Migration and Model

Here, we will update migration with adding new column date_of_birth for "users" table, let's update code on following file.

database/migrations/2014_10_12_000000_create_users_table.php

Next, run create new migration using laravel migration command as bellow:

Now we will update User model by using following command:

app/Models/User.php

Read Also: Laravel CRUD with Image Upload Tutorial

Step 3: Create Controller

In this step, we will create a new UserController; in this file, we will add two method create() and show() for creating new record and show record from database.

Let's create UserController by following command:

next, let's update the following code to Controller File.

app/Http/Controllers/UserController.php

Step 4: Create and Add Routes

Next, You have to open and update the following routes in the routes/web.php file.

routes/web.php

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

Output for Mutator:

Now, Go to your web browser, type the given URL and view the app output:

It will create user as like bellow screen shot:

Output for Accessor:

Now, Go to your web browser, type the given URL and view the app output:

It will create user as like bellow screen shot:

Read Also: Laravel 9 Multiple File Upload Tutorial

I hope it can help you...

Last updated

Was this helpful?