在 Laravel 5.1 中将模型保存到数据库之前做一些事情
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31993559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do something before saving model to database in Laravel 5.1
提问by ronin1184
How can I do something such as modify some data fields or more validate before writing data to database in Laravel 5.1 model ? It's document about that problem is hard to use in real application: http://laravel.com/docs/5.1/eloquent#events
在将数据写入 Laravel 5.1 模型中的数据库之前,我该如何做一些事情,例如修改某些数据字段或进行更多验证?关于该问题的文档很难在实际应用中使用:http: //laravel.com/docs/5.1/eloquent#events
My code is
我的代码是
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Helpers\Tools as Tools;
class Atoken extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'atoken';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'token',
'user_id',
'role',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
];
public static function newToken($userId, $role){
# Remove all token assoiciate with input user;
Atoken::where('user_id', $userId)->delete();
$params = [
'user_id' => $userId,
'role' => $role,
];
Atoken::insert($params);
$item = Atoken::where('user_id', $userId)->first();
return $item->token;
}
protected static function boot(){
static::creating(function ($model) {
$model->token = 'sometoken';
});
}
}
In this case, I always got error:
在这种情况下,我总是遇到错误:
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \"token\" violates not-null constraint (SQL: insert into \"atoken\" (\"user_id\", \"role\") values (2, USER))
How can I fix it?
我该如何解决?
回答by Joseph Silber
class Lunch extends Eloquent
{
protected static function boot()
{
static::creating(function ($model) {
$model->topping = 'Butter';
return $model->validate();
});
}
protected function validate()
{
// Obviously do real validation here :)
return rand(0, 1) ? true : false;
}
public static function newToken($userId, $role)
{
static::where('user_id', $userId)->delete();
return static::create([
'user_id' => $userId,
'role' => $role,
])->token;
}
}
回答by Yevgeniy Afanasyev
I would recommend to go into EventServiceProvider, and register event listeners
我建议进入 EventServiceProvider,并注册事件侦听器
public function boot(DispatcherContract $events)
{
parent::boot($events);
// Register Event Listeners
\App\Product::updating(function ($product) {
$product->onUpdating();
});
...
then create function onUpdating
within the model. You also can choose from saving, saved, creating, created, updating, updated..
然后onUpdating
在模型中创建函数。你也可以选择saving, saved, creating, created, updating, updated..
This documentation has more: https://laravel.com/docs/5.1/eloquent#events
本文档有更多内容:https: //laravel.com/docs/5.1/eloquent#events