让我有一个名为 customer
的表,其中 customer 表有一个名为 deleted_by
的字段。
我在 customer
模型中实现了 softDelete
。现在我想在行删除时更新 deleted_by
。这样我就可以追踪谁删除了这一行。
我确实在谷歌上搜索过它,但我没有找到任何东西。
我使用 laravel 4.2.8 和 Eloquent
最佳答案
您可以使用如下方式更新字段:
$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'));
}
此外,您可以在一个查询中完成此操作:
// 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);
两者均在一个查询中完成,softDelete
并记录 deleted_by
。
关于laravel - laravel中删除行时如何更新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25503004/