This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Somebody knows how to get mutators working.
In my model only accessors are working but not mutators.
For example:
This is not working
public function setDiscGlobalAttribute()
{
$this->attributes['disc_global'] = 88.88;
}
But the oposite is working:
public function getDiscGlobalAttribute($value)
{
$value = 3.33;
return $value;
}
Anybody knows what is going wrong?
Thanks in advance
Thank you for your reply,
unfortunately not, the function is definitely not called when the model gets saved.
when I going to write a "dump and die" within the accessor it generates output but not from within the mutator.
dd($this->attributes);
In plain Laravel it works !?!?
Kind regards
I created a mutator for one of my models and it's working fine.
public function setNameAttribute($value)
{
$this->attributes['name'] = strtoupper($value);
}
Maybe a plugin is interfering with your model? Try with a simple model from a clean install and see if that works.
This is a Model from a plugin I created with the Builder plugin.
The only things I updated within the model by hand are the two demo mutator and accessor functions.
Do you really think it could depend on other plugins ?
Just to be 100% sure of that: just rename the field name (and accessor) to whatever it could never be used elsewhere: such like setReallyDummyAttribute and in your fields.yaml, make the same changes.
I don't think another plugin inteferes here, can you copy/paste the fields.yaml? The problem is coming from elsewhere, your accessor and mutator are good.
And just in case: do you use any beforeSave/beforeCreate method in your model? If it's so, copy/paste it.
The model does not have a fields.yaml because it is not used in cms/backend it is only by my plugin's interface.
Here is the wohle model, beforeSave/beforeCreate methods:
<?php namespace Eq3w\Onboarding\Models;
use Model;
/**
* Model
*/
class FinInfo extends Model
{
use \October\Rain\Database\Traits\Validation;
use \October\Rain\Database\Traits\SoftDelete;
protected $dates = ['deleted_at'];
protected $guarded = ['id'];
/**
* @var string The database table used by the model.
*/
public $table = 'eq3w_onboarding_financial_information';
/**
* @var array Validation rules
*/
public $rules = [
];
public function setDiscGlobalAttribute()
{
if ($this->attributes['mod_global'] == 0)
{
$this->attributes['disc_global'] = 0.00;
}
}
// relations
public $belongsTo = [
'company' => ['Eq3w\Onboarding\Models\Company', 'key' => 'company_id', 'otherKey' => 'id']
];
}
I used mutators for operations like this several times before in Lavravel.
1-7 of 7