在Laravel 9中,对特定仪表的更新功能中进行唯一日期验证。

huangapple go评论62阅读模式
英文:

unique date validation in update function for a specific meter in laravel 9

问题

这是我的存储函数

public function store(Request $request)
{
    $validatedData = $request->validate( [
        'meter' => 'required',
        'date' => 'required|date_format:Y-m-d|unique:meterreading,date,NULL,meter,'.$request->get('meter'),
    ]);
}

日期对于特定的表计是唯一的。它正常工作。

如何在更新函数中验证此内容:

public function update(Request $request, MeterReading $meterreading)
{
    $updateData = $request->validate( [
        'date' => 'required|date|unique:meterreading,date,'.$meterreading->id.',meter,'.$request->get('meter'),
    ]);
}

在更新表单时,错误消息会出现为“日期已被占用”。如何清除此消息。感谢您!

英文:

This is my store function

public function store(Request $request)
{
    $validatedData = $request->validate( [
        'meter' => 'required',
        'date' => 'required|date_format:Y-m-d||unique:meterreading,date,NULL,meter,meter,'.$request->get('meter'),
    ]);
}

date is unique for a specific meter. Its working perfectly.

And how can i validate this in update function

public function update(Request $request, MeterReading $meterreading)
{
    $updateData = $request->validate( [
        'date' => 'required|date|unique:meterreading,date,'.$meterreading->id.',meter,meter,'.$request->get('meter'),
    ]);

While updating the form the error message comes as

The date has already been taken. How can i clear this.
TIA

答案1

得分: 0

public function update(Request $request, MeterReading $meterreading)
{
$updateData = $request->validate([
'date' => 'required|date|unique:meterreading,date,'.$meterreading->id.',id,meter,'.$meterreading->meter,
]);
}

英文:
public function update(Request $request, MeterReading $meterreading)
{
    $updateData = $request->validate([
        'date' => 'required|date|unique:meterreading,date,'.$meterreading->id.',id,meter,'.$meterreading->meter,
    ]);    
}

huangapple
  • 本文由 发表于 2023年3月7日 13:22:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定