0
我對Laravel相當陌生,我正在嘗試使用Model事件。 考慮一個模型具有這些事件(注意,手動拋出的異常):Laravel DB ::事務不會使用模型事件捕獲異常
class Doctor extends \Eloquent {
protected $fillable = ['name', 'summary', 'description', 'image'];
public static function boot() {
parent::boot();
Doctor::deleting(function(Doctor $doctor) {
$doctor->page()->delete();
});
Doctor::created(function(Doctor $doctor) {
throw new \Exception('hahahah!!!');
$doctor->page()->create(['title' => $doctor->name, 'slug' => $doctor->name]);
});
}
public function page() {
return $this->morphOne('Page', 'pageable');
}
}
該控制器的存儲方法:
public function store() {
// TODO : Extract Validation to a Service.
$validator = \Validator::make(
\Input::only('name'),
['name' => 'required']
);
if ($validator->fails()) {
return \Redirect::back()->withErrors($validator)->with(['items' => $this->dataArray]);
}
$doctor = \Doctor::create(\Input::all());
\DB::transaction(function() use($doctor) {
$doctor->save();
});
return \Redirect::route('Emc2.doctors.edit', ['id' => $doctor->id]);
}
的問題是,在DB ::交易犯規趕上拋出異常由模型,所以我不能回滾交易。
我做錯了什麼?
任何幫助將不勝感激! 謝謝!
絕對!多麼愚蠢的我:)謝謝! – 2014-09-22 15:17:48