laravel 在laravel中删除一行时如何更新字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25503004/
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
How to update field when delete a row in laravel
提问by The Alpha
Let I have a table named customer
where customer table has a field named deleted_by
.
I implement softDelete
in customer
model. Now I want to update deleted_by
when row delete. So that I can trace who delete this row.
让我有一个名为的表customer
,其中客户表有一个名为deleted_by
. 我softDelete
在customer
模型中实现。现在我想deleted_by
在行删除时更新。这样我就可以追踪谁删除了这一行。
I do search on google about it But I don't found anything.
我确实在谷歌上搜索过它,但我什么也没找到。
I use laravel 4.2.8 & Eloquent
我使用 laravel 4.2.8 & Eloquent
回答by The Alpha
You may update the field using something like this:
您可以使用以下内容更新该字段:
$customer = Customer::find(1); // Assume 1 is the customer id
if($customer->delete()) { // If softdeleted
DB::table('customer')->where('id', $customer->id)
->update(array('deleted_by' => 'SomeNameOrUserID'));
}
Also, you may do it in one query:
此外,您可以在一个查询中完成:
// Assumed you have passed the id to the method in $id
$ts = Carbon\Carbon::now()->toDateTimeString();
$data = array('deleted_at' => $ts, 'deleted_by' => Auth::user()->id);
DB::table('customer')->where('id', $id)->update($data);
Both is done within one query, softDelete
and recorded deleted_by
as well.
两者都在一个查询中完成,softDelete
并且也被记录下来deleted_by
。
回答by Jarek Tkaczyk
Something like this is the way to go:
这样的事情是要走的路:
// override soft deleting trait method on the model, base model
// or new trait - whatever suits you
protected function runSoftDelete()
{
$query = $this->newQuery()->where($this->getKeyName(), $this->getKey());
$this->{$this->getDeletedAtColumn()} = $time = $this->freshTimestamp();
$deleted_by = (Auth::id()) ?: null;
$query->update(array(
$this->getDeletedAtColumn() => $this->fromDateTime($time),
'deleted_by' => $deleted_by
));
}
Then all you need is:
那么你只需要:
$someModel->delete();
and it's done.
它完成了。
回答by Marwelln
I would rather use a Model Eventfor this.
我宁愿为此使用模型事件。
<?php
class Customer extends \Eloquent {
...
public static function boot() {
parent::boot();
// We set the deleted_by attribute before deleted event so we doesn't get an error if Customer was deleted by force (without soft delete).
static::deleting(function($model){
$model->deleted_by = Auth::user()->id;
$model->save();
});
}
...
}
Then you just delete it like you would normally do.
然后您只需像往常一样删除它。
Customer::find(1)->delete();
回答by Sekonda
I know this is an old question, but what you could do (in the customer model) is the following....
我知道这是一个老问题,但是您可以(在客户模型中)执行以下操作....
public function delete()
{
$this->deleted_by = auth()->user()->getKey();
$this->save();
return parent::delete();
}
That would still allow the soft delete while setting another value just before it deletes.
这仍然允许软删除,同时在删除之前设置另一个值。