如何在 laravel 中获取更新的记录 ID?
how to get updated records ids in laravel?
我正在更新 laravel 中的多行。
StoragePeriod::where('customer_id', $invoice->customer_id)->update(['invoice_id' => $invoice->id]);
如何获取这些更新记录的 ID?
您可以使用tap方法。
如果您希望获取所有模型而不仅仅是 ID,您可以将 pluck
替换为 get
。
tap(StoragePeriod::where('customer_id', $invoice->customer_id))
->update(['invoice_id' => $invoice->id])
->pluck('id');
来自文档:
If no closure is passed to the tap function, you may call any method
on the given $value. The return value of the method you call will
always be $value, regardless of what the method actually returns in
its definition. For example, the Eloquent update method typically
returns an integer. However, we can force the method to return the
model itself by chaining the update method call through the tap
function:
$user = tap($user)->update([
'name' => $name,
'email' => $email,
]);
我正在更新 laravel 中的多行。
StoragePeriod::where('customer_id', $invoice->customer_id)->update(['invoice_id' => $invoice->id]);
如何获取这些更新记录的 ID?
您可以使用tap方法。
如果您希望获取所有模型而不仅仅是 ID,您可以将 pluck
替换为 get
。
tap(StoragePeriod::where('customer_id', $invoice->customer_id))
->update(['invoice_id' => $invoice->id])
->pluck('id');
来自文档:
If no closure is passed to the tap function, you may call any method on the given $value. The return value of the method you call will always be $value, regardless of what the method actually returns in its definition. For example, the Eloquent update method typically returns an integer. However, we can force the method to return the model itself by chaining the update method call through the tap function:
$user = tap($user)->update([ 'name' => $name, 'email' => $email, ]);