Enter and search data in the database per user with Laravel

Asked

Viewed 582 times

2

I need to save information in the database per logged-in user. That is, when the user logs in, everything he does will be saved in the database of that login.

That is, when user X logs in, everything he saves in the bank will be related to it (user X).

How to proceed in this case in Laravel? I thought to be by ID, Auth, etc...

1 answer

1

Dear friend I suggest you to create a table of logs where you bind by id user to the activity performed by the user. Example:

  1. Create a Model for the Logs table.
  2. Create a Helper that you can call on any Controller.
  3. Create default descriptions for each activity be it one insert, update among others...

Example Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class SystemLogs extends Model
{

}

Example Helper:

<?php 

namespace App\Helpers;

class LogsHelper
{
    public function saveLog($acivity){
      ...
    }
}   

Example Controller:

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use App\Helpers\LogsHelper;

class MyController extends BaseController
{
  public function add(){
    ...
  }

  public function save(){
    ...
  }

}

Of course I’m not going to program for you, I’m just passing on an idea of logging very simple user activities. In this case you can save in the log table the name of the changed table, the id of the record created, changed or deleted linked to the user id. This is very useful in audits.

  • Thank you very much, it worked! I tested this logic with what I had thought and it worked, thank you!

  • @brunoelyg would have how to share his code? I understood the logic but as I’m beginner in Laravel I can’t get out of place. :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.