简体   繁体   中英

How to ignore mutators and accessors when required

I used Laraval's Accessors and Mutators to essentially overwrite the group column on my table by doing this on my model;

public function getGroupAttribute($value)
{
    if(config('app.pages.'.$value)) {
        return $value;
    }
    return "city";
}

So if $value is in the config, return $value else return the default.

That works fine. The only problem is now I am using Maatwebsite to export the dataset I actually need the real value stored in the database. And at the moment, it doesn't do that, it only returns group after it's been "mutated" via the above method.

What can I do?

I think that override a standard property is not a good thing.

You can create your own mutator using a different name from the property, for example getComputerGroupAttribute , so you could use original members of the object when you need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM