场景:学生->家长,学生的子类
Parent: Student Child: Marks Address Grand Child Primary Address Secondary Address We are using the Cascade Soft-Delete for the above scenario with the code below : 这里有多个删除发生,需要建议了解我们是否必须为级联删除添加事务管理。
use Illuminate\Database\Eloquent\SoftDeletes;
class Student extends Model
{
use SoftDeletes;
protected static function boot()
{
parent::boot();
static::deleting(function ($student) {
foreach ([
'address',
'mark'
] as $relation) {
foreach ($student->{$relation} as $item) {
$item->delete();
}
}
});
}
public function address()
{
return $this->hasMany(StudentAddress::class, 'student');
}
public function mark()
{
return $this->hasMany(Marks::class, 'mark_detail');
}
}发布于 2019-04-15 19:19:55
对于在Laravel中添加事务,请检查以下代码
\DB::transaction(function () {
Your queried goes here
});或
DB::beginTransaction();
Your queried goes here
DB::commit();https://stackoverflow.com/questions/55685082
复制相似问题