[英]Update column automatically based on value of another column (same table) in Laravel (7)
I am a newbe of Laravel. This is my (pivot) table:我是 Laravel 的新手。这是我的(数据透视)表:
id | user_id | sponsorship_id | created_at | updated_at | end_date
I would like to populate the end_date column automatically based on the value of the created_at column with these conditions:我想根据具有以下条件的 created_at 列的值自动填充 end_date 列:
if sponsorship_id == 1 add 24 hours to the created_at and put the new date in the end_date column如果 sponsorship_id == 1 将 24 小时添加到 created_at 并将新日期放入 end_date 列
if sponsorship_id == 2 add 72 hours to the created_at and put the new date in the end_date column如果 sponsorship_id == 2 将 72 小时添加到 created_at 并将新日期放入 end_date 列
if sponsorship_id == 3 add 144 hours to the created_at and put the new date in the end_date column如果 sponsorship_id == 3 将 144 小时添加到 created_at 并将新日期放入 end_date 列
This is currently in my controller:这是目前在我的 controller 中:
public function updateSponsorship(Request $req, User $user){
$dataSponsor = $req->all();
$new_user = new User();
$new_user->fill($dataSponsor);
$user->update($dataSponsor);
if(array_key_exists('sponsorships', $dataSponsor)){
$user->sponsorships()->attach($dataSponsor['sponsorships']);
} else {
$user->sponsorships()->detach();
}
return redirect()->route('admin.users.show', $user);
}`
Data is sent through a FORM from edit.blade.php数据通过 FORM 从 edit.blade.php 发送
Any suggestion??有什么建议吗??
Thanks!谢谢!
It makes use of eloquent events to set the 'end_date' attribute of your model它利用 eloquent 事件来设置 model 的“end_date”属性
you can check the below link for more info您可以查看以下链接以获取更多信息
https://stackoverflow.com/a/27936307/3739625 https://stackoverflow.com/a/27936307/3739625
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.