6

I have a list of date fields, and all of them have the same logic in their mutators. I would like to extract this functionality to a trait so that in the future all I would need is to create an array of date fields in the model and use the trait.

Something like this:

foreach( $dates as $date ) {
    $dateCamelCase = $this->dashesToUpperCase($date);
    $setDateFunctionName ='set'.$dateCamelCase.'Attribute';
    $this->{$setDateFunctionName} = function()  use($date) {
        $this->attributes[$date] = date( 'Y-m-d', strtotime( $date ));
    };
}
sepehr
  • 13,648
  • 5
  • 73
  • 111
shaswa
  • 215
  • 4
  • 14

1 Answers1

11

Before answering your specific question, let's first see how Eloquent mutators work.

How eloquent mutators work

All Eloquent Model-derived classes have their __set() and offsetSet() methods to call the setAttribute method which takes care of setting the attribute value and mutating it, if needed.

Before setting the value, it checks for:

  • Custom mutator methods
  • Date fields
  • JSON castables and fields

Tapping into the process

By understanding this, we can simply tap into the process and overload it with our own custom logic. Here's an implementation:

<?php

namespace App\Models\Concerns;

use Illuminate\Database\Eloquent\Concerns\HasAttributes;

trait MutatesDatesOrWhatever
{
    public function setAttribute($key, $value)
    {
        // Custom mutation logic goes here before falling back to framework's 
        // implementation. 
        //
        // In your case, you need to check for date fields and mutate them 
        // as you wish. I assume you have put your date field names in the 
        // `$dates` model property and so we can utilize Laravel's own 
        // `isDateAttribute()` method here.
        //
        if ($value && $this->isDateAttribute($key)) {
            $value = date('Y-m-d', strtotime($value));
        }

        // Handover the rest to Laravel's own setAttribute(), so that other
        // mutators will remain intact...
        return parent::setAttribute($key, $value);
    }
}

Needless to say that your models require to use this trait to enable the functionality.

You ain't gonna need it

If mutating dates is the only usecase you need to have "dynamically named mutators", that's not required at all. As you might have already noticed, Eloquent's date fields can be reformatted by Laravel itself:

class Whatever extends Model 
{
    protected $dates = [
        'date_field_1', 
        'date_field_2', 
        // ...
    ];

    protected $dateFormat = 'Y-m-d';
}

All fields listed there will be formatted as per $dateFormat. Let's not reinvent the wheel then.

Community
  • 1
  • 1
sepehr
  • 13,648
  • 5
  • 73
  • 111
  • In case one needs to reuse accessors/mutators, you might be interested in Eloquent Mutators https://github.com/topclaudy/eloquent-mutators – topclaudy Dec 14 '18 at 18:08
  • you can also define casts in your model, wich is short and elegant: protected $casts = [ 'your_field' => 'datetime:d/m/Y H:i:s', ]; – manuel-84 Oct 20 '19 at 13:25
  • Creating multiple dynamic mutator Traits will call model parent::setAttribute multiple times? – Manpreet Dec 13 '19 at 01:37