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