Creating dynamically named mutators in Laravel Eloquent models
Asked Answered
C

1

8

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 ));
    };
}
Crossopterygian answered 3/5, 2017 at 14:25 Comment(4)
show some code, and your current attemptsGoalkeeper
added the way I triedCrossopterygian
@Crossopterygian Were you able to solve it? I am having a similar situation and trying the same. Please let me know if you could work it out.Scend
@Scend : I haven't been able to. Due to a time crunch, had to define a mutator for each of the fields in the Model.Crossopterygian
E
14

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.

Evert answered 21/1, 2018 at 21:23 Comment(3)
In case one needs to reuse accessors/mutators, you might be interested in Eloquent Mutators github.com/topclaudy/eloquent-mutatorsTysontyumen
you can also define casts in your model, wich is short and elegant: protected $casts = [ 'your_field' => 'datetime:d/m/Y H:i:s', ];Stopcock
Creating multiple dynamic mutator Traits will call model parent::setAttribute multiple times?Toothless

© 2022 - 2024 — McMap. All rights reserved.