Laravel 5.4 Eloquent 未保存某些字段
Laravel 5.4 Eloquent not Saving certain fields
我一直致力于在模型事件中记录所有用户活动...但由于某种原因,记录存储在 user_action table 而不是 action_model 字段中。这是我的代码。
用户操作 Class
`
class UserAction extends Model
{
use SoftDeletes;
protected $table = 'user_actions';
/**
*
*/
protected $fillable = [
'user_id','action', ' action_model', 'action_id'
];
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
`
UserActionObervable
class UserActionObserver
{
public function saved($model)
{
$type = $model->getTable();
// dd($type); when dump the value exists
if ($model->wasRecentlyCreated == true) {
// Data was just created
$action = 'created';
} else {
// Data was updated
$action = 'updated';
}
if (Sentinel::check()) {
UserAction::create([
'user_id' => Sentinel::getUser()->id,
'action' => $action,
'action_model' => 'model', //I tried to pass in constant
'action_id' => $model->id
]);
}
}
public function deleting($model)
{
if (Sentinel::check()) {
UserAction::create([
'user_id' => Sentinel::getUser()->id,
'action' => 'deleted',
'action_model' => $model->getTable(),
'action_id' => $model->id
]);
}
}
}
这是架构
Schema::create('user_actions', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->string('action'); // created / updated / deleted
$table->string('action_model');
$table->integer('action_id')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
$table->foreign('user_id')->references('id')
->on('users')->onDelete('cascade');
});
protected $fillable = [
'user_id','action', ' action_model', 'action_id'
];
尝试删除 action_model
之前的 space。 $fillable
中的字段与您传递给 create()
方法的键不匹配。
我一直致力于在模型事件中记录所有用户活动...但由于某种原因,记录存储在 user_action table 而不是 action_model 字段中。这是我的代码。 用户操作 Class `
class UserAction extends Model
{
use SoftDeletes;
protected $table = 'user_actions';
/**
*
*/
protected $fillable = [
'user_id','action', ' action_model', 'action_id'
];
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
`
UserActionObervable
class UserActionObserver
{
public function saved($model)
{
$type = $model->getTable();
// dd($type); when dump the value exists
if ($model->wasRecentlyCreated == true) {
// Data was just created
$action = 'created';
} else {
// Data was updated
$action = 'updated';
}
if (Sentinel::check()) {
UserAction::create([
'user_id' => Sentinel::getUser()->id,
'action' => $action,
'action_model' => 'model', //I tried to pass in constant
'action_id' => $model->id
]);
}
}
public function deleting($model)
{
if (Sentinel::check()) {
UserAction::create([
'user_id' => Sentinel::getUser()->id,
'action' => 'deleted',
'action_model' => $model->getTable(),
'action_id' => $model->id
]);
}
}
}
这是架构
Schema::create('user_actions', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->string('action'); // created / updated / deleted
$table->string('action_model');
$table->integer('action_id')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
$table->foreign('user_id')->references('id')
->on('users')->onDelete('cascade');
});
protected $fillable = [
'user_id','action', ' action_model', 'action_id'
];
尝试删除 action_model
之前的 space。 $fillable
中的字段与您传递给 create()
方法的键不匹配。