What is the best way to set an attribute on a model without saving it to the database?

daviesgeek - 5 months ago

I'm working with sensitive data (credit card numbers), and I'd like to be able to set the card number on a model (this is from inside a FieldTypeAccessor) without having the model try to save the attribute into the database, (which results in an SQL error).

What's is the best to to do this? It seems like this is probably more of a Laravel question, but I'm not really sure how to go about this.

Answer

- 5 months ago

Just keep it in the FT and get it from there when you need it. OOP means it will be there and carry along with the model.

$entry->getFieldType('card')->getMyPrivateStuff();

ryanthompson - 5 months ago

If you define the property on the model it will not save. Considering it's private I would make it a protected attribute and use setter / getters.

daviesgeek - 5 months ago

Sorry I guess I didn't do a good job of explaining this.

I have a field type that I'm working on, that has multiple properties. One of these properties is one that I'd like to be able to keep around when it gets posted via a form and passed around in my model observer, but I don't ever want to save that value. The way I'm currently doing it in the field type accessor (using setRawAttributes) results in the saving throwing an SQL error because the column doesn't exist in the database .

ryanthompson - 5 months ago

Just keep it in the FT and get it from there when you need it. OOP means it will be there and carry along with the model.

$entry->getFieldType('card')->getMyPrivateStuff();

daviesgeek - 5 months ago

Okay great, that works to access it, but how would I actually set the property inside the accessor?

ryanthompson - 5 months ago

The same way. You have the field type right there in the accessor.