Laravel:是否可以在数据库字段中存储对象?如果是这样,怎么办?

Suppose I have an User, and a Transaction model on Laravel. It doesn't matter the properties, they only have in common an uuid and a name field

Now I want to create an Audit model which will take more or less the following fields:

id       - The regular autoincrement id
ts       - The timestamp when the action is performed
actor    - The one who performs an action, its a reference to a user's id
verb     - Which action is performing over a certain object
object   - The object to which the action is performed
comment  - whatever

因此,如果管理员创建o更新,则新用户将产生审核记录,例如

1, 1/1/2021 00:00:01, 1, 'create', User, 'bla bla bla'
2, 1/1/2021 00:00:02, 1, 'update', User, 'reset password'

But that will also apply to other kind of records like the Transaction I mentioned before

目前,我正在存储对象的ID,并且基于“动词”(或动作),我知道它们所作用的对象的类型,并且它可以工作,但是我正在尝试是否还有其他方法。我的数据库不是很大,因此性能不是问题。

评论
Ao初见
Ao初见

存储对象序列化XML,JSON。

点赞
评论
xnam
xnam

The best way to store an object like that in a database is in a normal form.

I'd suggest creating a new migration and model for your audit, and storing all the relevant information there.

However, you can serialize your object as a string if you don't have any performance concerns, or if the data structure is likely to change. A popular way of doing this is to use json_encode($your_obj).

点赞
评论