0

I have built an e-commerce website where the admin can edit the products through MySQL. Now I want to display the edit history of each product. The edit history should contain the 'date when it was updated' and 'what was updated'.

This is my product table: Click here to see the table

I thought of creating new columns "Edit history" and "Edit Date" inside the product table. Whenever the admin would edit one column, it would get added here.

If anyone knows how to proceed with either this method or some alternative method to solve this issue, please let me know.

TylerH
  • 20,799
  • 66
  • 75
  • 101
smrithi
  • 67
  • 8
  • Opinion based questions are not allowed on SO. – Gert B. Jan 25 '22 at 14:37
  • `the admin can edit the products through mysql` this sounds... awful. You may find this relevant: https://stackoverflow.com/questions/6787794/how-to-log-all-changes-in-a-mysql-table-to-a-second-one . As @GertB. said this is off topic as asked. – AD7six Jan 25 '22 at 14:46
  • @AD7six I hope he was not talking about using direct queries but using the admin panel :-D – Gert B. Jan 25 '22 at 14:49
  • @GertB. What is opinion-based about this question? – TylerH Jan 25 '22 at 19:27
  • 1
    @TylerH The OP is asking for advise on the best way for handeling history on models. The best way to handle this is an opinion. It's not a coding question but a how-to question. That is off-topic. – Gert B. Jan 26 '22 at 06:27
  • @GertB. The OP is clearly asking _how_ to do something, not advice on the best way to do it. No opinion-based qualifiers exist in the question whatsoever. Please don't recommend incorrect close reasons. – TylerH Jan 26 '22 at 14:34
  • @AD7six Almost any question on Stack Overflow can be answered in more than one way. That does not make the _question_ opinion-*based*. I agree the question is in need of more detail/scope, but that's not relevant to whether it is opinion-based. – TylerH Jan 26 '22 at 14:35
  • @TylerH I don't recommend any close reasons, I explain why I think a question is off-topic and why I flagged it as so. The flag is accepted, and I'm not the only one who thinks it's off-topic and opinion based. If my flag is incorrect, it will be declined. If too many flags of mine are declined, I wil not be able to flag another post. You can disagree with me, but you can't tell I can't flag a post that I believe should be closed. – Gert B. Jan 26 '22 at 14:46
  • @GertB. I'm not telling you not to flag, I'm asking you to use accurate flags and not mislead askers with incorrect comments. This is not an opinion-based question (I would know, I've voted to close some 40,000 questions as opinion-based in my time here). – TylerH Jan 26 '22 at 14:52
  • @TylerH As I commented.. My flag is not declined but marked as helpful. So at least one person with the required rep shares my opinion. So we disagree. case closed. – Gert B. Jan 26 '22 at 14:59
  • @GertB. People get stuff wrong all the time; that's not what I'm addressing. You left a comment suggesting this was opinion-based. That comment is incorrect. – TylerH Jan 26 '22 at 16:14
  • @AD7six I would not answer the question because it needs more detail/scope. I'm simply trying to make sure people don't continue closing things for the wrong reasons. If this question were better-scoped and had more detail, it would not be close-worthy _at all_, which is the point. People closing something for reason X is not a huge deal if it is truly closeable for reason Y. But it can become a big deal if those people continue thinking reason X was accurate, because then they'll go around closing other non-opinion-based questions as opinion-based, causing harm to the site and confusing users – TylerH Jan 26 '22 at 16:16

3 Answers3

1

You could create a Trigger in SQL which would allow that if there is a modification you add automatically in your table the data. I let you look at this course if you want : Sql trigger or Sql Trigger. Then you add for the date a Now for the date of the update (the same day) and for the modifications in your trigger add some SQL to see the difference between the before and after. Also, as mentioned above, creating an Audit table would allow you to save your changes. Your triggers would make it easier to add rows. Good luck to you

x300
  • 28
  • 8
0

I would create another table that keeps track of the history, product_history with columns (id, product_id, from (json), to (json), created_at, updated_at).

In the from and to columns just encode the complete content of the old and new row, you could / should do this in a observer.

frogeyedman
  • 534
  • 1
  • 5
  • 23
0

one of solutions is use this in products table:

$table->text('history');

when you update the product take the id and store in history the current date and the data like this

{"date":"day-month-year","data":"some data updated bla bla.."}

when you update this put another object in same row of product_id

Ossama Abd
  • 464
  • 2
  • 10