laravel - Laravel - updateOrCreate or a sync method?
问题描述
I'm trying to see what my best route should be but for the sake of ease I will make this sound much simpler than it is.
I have two models, one called donor
and one called donation
. For the relationships, the donor
has many donations
.
In my edit form of the donor
model, I have the ability to create, update or delete the donations
from the same form.
Updating and creating are easy at the moment because I can use updateOrCreate. But what happens if I want to delete a donation?
Would I perform an actual query filtering out the ids of the donations that were still on the edit form (and therefore not deleted by the user) and then delete the models from there? Or is there a better way of handling this action?
Thanks in advance.
解决方案
在您的DonationController中,您可以使用如下查询选择要删除的捐赠:
public function destroy($id)
{
$donation = Donation::where('donor_id','=', $id)->get();
$donation->delete();
}
在您看来,您可以制作一个删除表单,然后他们将他发送到控制器中的此功能。我认为最好的方法是做一个 foreach 并循环内部捐赠者的捐款。
@foreach($donor->donations as $donation)
{
<form method="POST" action="{{route('donor.destroy', $donation->id)}}">
<button type="submit">Delete {{$donation->id}}</button>
</form>
}
@endforeach
推荐阅读
- python-3.x - 如何忽略 sympy.solve 复杂的解决方案
- python - Jupyter Book,如何更改字体或间距(文本格式)
- javascript - 使用反应上下文的问题
- c++ - Memcpy 数据直接存入 std::vector
- ssis - SSIS Azure 工具在 VS2019 Azure SSIS 工具箱中显示为灰色
- spring-boot - 网关处的 Prometheus 指标:未知而不是 uri 中的路径
- spring-boot - Spring MVC + Elastic Beanstalk | 找不到嵌套在目录中的模板
- python - 使用 Openpyxl 进行迭代时出现 MemoryLoadError
- assembly - 汇编OFFSET在一条指令中使用了两次,什么是offset的偏移量?
- c++ - a%2 和 a&1 哪个更快?