gpt4 book ai didi

php - 使用 laravel php-mysql 在一段时间内仅更改一次字段值

转载 作者:行者123 更新时间:2023-11-30 22:13:00 28 4
gpt4 key购买 nike

我有一个表,其中包含一些属性,包括 timestamp ,当用户第一次输入 invoice 值时,该表包含有关发票的字段我想更改值在他添加项目后 24 小时 内只有一次发票!我想阻止他在发票超过 24 小时 时进行任何更改,我使用 laravel 框架

最佳答案

在您的发票模型上,您需要添加一个 protected $timestamp 设置为 true;

然后,当创建或更新记录时,将自动填充字段 created_at/updated at。

所以在 laravel 中有一个处理日期时间的特性是一个名为 Carbon 的库。

正如 tadman 和 Khalid 在您的 Controller 中建议的那样,您可以:

<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use Invoice;

class FooInvoiceController extends Controller
{
/**
*
* Method to check if invoice is editable
*
* @param $invoiceId
* @return bool
*/
public function isInvoiceEditable($invoiceId)
{

$now = Carbon::now();

$invoiceCreatedAt = Invoice::where('id', $invoiceId)->get()->created_at;

$diffInHours = Carbon::parse($invoiceCreatedAt);

$length = $diffInHours->diffInHours($now);

if ($length > 24) {
return false;
}

return true;
}
/**
* @param $invoiceId
*/
public function fooAction($invoiceId)
{
if ($this->isInvoiceEditable($invoiceId)) {
// Work here
}
}

}

关于php - 使用 laravel php-mysql 在一段时间内仅更改一次字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441814/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com