Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在不同的表中添加审核日志?_Php_Laravel - Fatal编程技术网

Php 如何在不同的表中添加审核日志?

Php 如何在不同的表中添加审核日志?,php,laravel,Php,Laravel,我一直在使用事件和侦听器,但最终只更新了users表 我试图将侦听器中的句柄更改为,$event->log->description='something';但是上面说 从空值创建默认对象 发生此问题是因为您试图将属性分配给不存在的$event->log对象 但是,要记录事件,您只需使用查询生成器在logs表中插入一条新记录即可 例如: <?php namespace App\Listeners; use Carbon\Carbon; use Illuminate\Auth\Event

我一直在使用事件和侦听器,但最终只更新了users表

我试图将侦听器中的句柄更改为,$event->log->description='something';但是上面说

从空值创建默认对象


发生此问题是因为您试图将属性分配给不存在的
$event->log
对象

但是,要记录事件,您只需使用查询生成器在
logs
表中插入一条新记录即可

例如:

<?php

namespace App\Listeners;

use Carbon\Carbon;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB; 

class LogSuccessfulLogin
{

    public function __construct()
    {
        //
    }

    public function handle(Login $event)
    {
        $event->user->current_sign_in_at = Carbon::now();
        $event->user->save();

        $this->logEvent($event); 
    }

    private function logEvent(Login $event)
    {
        return DB::table('logs')
            ->insert([
                'user_id' => $event->user->id, 
                'description' => 'Some description', 
                'action' => 'login'
            ]);
    }
}

我已经想了好几天了,但是我没有意识到我可以用一个查询生成器来解决这个问题。它对我有用。非常感谢。
<?php

namespace App\Listeners;

use Carbon\Carbon;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\DB; 

class LogSuccessfulLogin
{

    public function __construct()
    {
        //
    }

    public function handle(Login $event)
    {
        $event->user->current_sign_in_at = Carbon::now();
        $event->user->save();

        $this->logEvent($event); 
    }

    private function logEvent(Login $event)
    {
        return DB::table('logs')
            ->insert([
                'user_id' => $event->user->id, 
                'description' => 'Some description', 
                'action' => 'login'
            ]);
    }
}