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.

4
  • Your question is not clear. Can you explain a bit ? Commented Apr 21, 2015 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. Commented Apr 21, 2015 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. Commented Apr 21, 2015 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 Commented Apr 21, 2015 at 16:18

1 Answer 1

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.

2
  • 1
    You could also use a polymorphic relationship between the primary models and the log model.
    – Kryten
    Commented Apr 24, 2015 at 21:16
  • Yeah, that could be another approach :) Commented Apr 24, 2015 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.