我正在使用 laravel 5.4 来创建一个网络应用。
我创建了一个特征来实现创建,更新,删除和恢复的雄辩事件的事件。
我创建了一个特征,如下所示:
<?php
namespace App\Traits;
use Auth;
use App\Master\Activity;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
/**
* Class ModelEventLogger
* @package App\Traits
*
* Automatically Log Add, Update, Delete events of Model.
*/
trait ActivityLogger {
/**
* Automatically boot with Model, and register Events handler.
*/
protected static function boot()
{
parent::boot();
foreach (static::getRecordActivityEvents() as $eventName) {
static::$eventName(function (Model $model) use ($eventName) {
try {
$reflect = new \ReflectionClass($model);
return Activity::create([
'user_id' => Auth::user()->id,
'content_id' => $model->id,
'content_type' => get_class($model),
'action' => static::getActionName($eventName),
'description' => ucfirst($eventName) . " a " . $reflect->getShortName(),
'details' => json_encode($model->getDirty()),
'ip_address' => Request::ip()
]);
} catch (\Exception $e) {
Log::debug($e->getMessage());//return true;
}
});
}
}
/**
* Set the default events to be recorded if the $recordEvents
* property does not exist on the model.
*
* @return array
*/
protected static function getRecordActivityEvents()
{
if (isset(static::$recordEvents)) {
return static::$recordEvents;
}
return [
'created',
'updated',
'deleted',
'restored'
];
}
/**
* Return Suitable action name for Supplied Event
*
* @param $event
* @return string
*/
protected static function getActionName($event)
{
switch (strtolower($event)) {
case 'created':
return 'create';
break;
case 'updated':
return 'update';
break;
case 'deleted':
return 'delete';
break;
case 'restored':
return 'restore';
break;
default:
return 'unknown';
}
}
}
但是当我在模型中实现它时,如:
<?php
namespace App\Master;
use App\Traits\ActivityLogger;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class LeadSource extends Model
{
use ActivityLogger;
use SoftDeletes;
protected $table = 'lead_source';
protected $primaryKey = 'lead_source_id';
protected $dates = ['deleted_at'];
protected $fillable = [
'name', 'created_by', 'created_ip', 'updated_by', 'updated_ip'
];
}
然后在我的控制器中,我通过雄辩的模型照常创建/更新。但是事件没有被触发,也没有在活动表中记录任何内容。
以下是活动表的迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateActivityTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('activity', function (Blueprint $table) {
$table->increments('activity_id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('content_id');
$table->string('content_type', 255);
$table->string('action', 255);
$table->text('description')->nullable();
$table->longText('details')->nullable();
$table->ipAddress('ip_address')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('activity');
}
}
请告知问题是什么?
答案 0 :(得分:2)
我找到了解决方案,模型和迁移都没问题,这是它产生问题的特征。有没有。事情错了,这阻碍了它正常运作。
最重要的是错误的是Log,我做了;包括适当的类,这导致了问题。
以下是特征文件的更正代码。
<?php
namespace App\Traits;
use Auth;
use Request;
use App\Master\Activity;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
/**
* Class ModelEventLogger
* @package App\Traits
*
* Automatically Log Add, Update, Delete events of Model.
*/
trait ActivityLogger {
/**
* Automatically boot with Model, and register Events handler.
*/
protected static function bootActivityLogger()
{
foreach (static::getRecordActivityEvents() as $eventName) {
static::$eventName(function ($model) use ($eventName) {
try {
$reflect = new \ReflectionClass($model);
return Activity::create([
'user_id' => Auth::id(),
'content_id' => $model->attributes[$model->primaryKey],
'content_type' => get_class($model),
'action' => static::getActionName($eventName),
'description' => ucfirst($eventName) . " a " . $reflect->getShortName(),
'details' => json_encode($model->getDirty()),
'ip_address' => Request::ip()
]);
} catch (\Exception $e) {
Log::debug($e->getMessage());
}
});
}
}
/**
* Set the default events to be recorded if the $recordEvents
* property does not exist on the model.
*
* @return array
*/
protected static function getRecordActivityEvents()
{
if (isset(static::$recordEvents)) {
return static::$recordEvents;
}
return [
'created',
'updated',
'deleted',
'restored'
];
}
/**
* Return Suitable action name for Supplied Event
*
* @param $event
* @return string
*/
protected static function getActionName($event)
{
switch (strtolower($event)) {
case 'created':
return 'create';
break;
case 'updated':
return 'update';
break;
case 'deleted':
return 'delete';
break;
case 'restored':
return 'restore';
break;
default:
return 'unknown';
}
}
}
请检查并告知是否有任何错误或可以更好地完成。