Insert and fetch data in the database per user with Laravel

2

I need to save information to the database by logged in user. That is, when the user logs in, everything he does will save to the login database.

That is, when user X logs in, everything he saves in the database will refer to him (user X).

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

    
asked by anonymous 18.02.2017 / 22:29

1 answer

1

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

  • Create a Model for the Logs table.
  • Create a Helper that you can call anywhere Controller .
  • Creating default descriptions for each activity is a 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 will not program for you, I'm just passing on a very simple user activity log idea. In this specific case you can save in the table of logs the name of the changed table, the id of the record created, changed or deleted linked to the id of the user. This is very useful in auditing.

        
    19.02.2017 / 07:33