5

I am currently developing an application with Laravel 5, my main tables are users, suppliers, manufacturers etc.

Each of these tables has a separate users_log,suppliers_log log table. The purpose of these log tables is to review operations performed for a single entity (for example: logs belonging to a particular manufacturer and the changes made in the past for that manufacturer etc.,)

I am planning to use Eloquent and I have generated an Eloquent model for each of the major tables.

My Question : Should I generate separate Eloquent models for each of the log tables or just write a method like user_log() in the model of the major table to write my log.

P.S : Number of users going to use my application are few, therefore writing logs to a database is preferred over file logs.

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
Magesh Kumaar
  • 1,485
  • 2
  • 10
  • 29
  • Your question is not clear. Can you explain a bit ? – Noman Ur Rehman Apr 21 '15 at 16:14
  • I'm not sure there is a "right" answer here. The best we can hope for is to find one that fits your situation better than another, but either approach is acceptable. The result will be an opinionated answer, which is generally off-topic for StackOverflow. – George Cummins Apr 21 '15 at 16:14
  • @NomanUrRehman : I apologise for being unclear. My question should I create new `eloquent model` for `user_log` table or just use the `user` model and write a function to do the logging operation. – Magesh Kumaar Apr 21 '15 at 16:17
  • 1
    @GeorgeCummins : So I'll just write a method like `saveLog()` and use some `DB::` to write my log because that would be easy for me to perform rather than creating a new eloquent model – Magesh Kumaar Apr 21 '15 at 16:18

1 Answers1

4

I would only use a logs table, because logs should be something simple/generic/straightforward.

These are the logs table columns I would use:

  • id
  • user_id (User that performed the action we're logging)
  • action (description of the action performed: deleted user, updated supplier, created manufacturer, etc)
  • url (the URL used to perform the action)
  • ip (IP address of the user)
  • timestamps

With that in mind, you would then have a standard pivot table for each loggable table type, which in your case would be log_manufacturer, log_supplier and log_user (I'm following the Laravel table naming convention here, but go ahead and use other names if you wish).

This way you can do a global search by any kind of log:

$logs = Log::where('user_id', 1)->get(); // get all logs of User 1

by specific log type (user, supplier, manufacturer, etc):

$logs = Log::has('suppliers')
            ->where('user_id', 1)
            ->get(); // get Supplier logs created by User 1

or get all specific models filtered by a Log:

$manufacturers = Manufacturer::whereHas('logs', function ($query)
{
    $query->where('user_id', 1);
})
->get(); // get all Manufacturers that have been manipulated by User 1

In order to create a new entry on the logs table, I would then use Model Observers so that at each created, updated or deleted action it would handle the creation transparently.

Quetzy Garcia
  • 1,820
  • 1
  • 21
  • 23