[英]Nullable and Unique is not working on laravel validation
我想保持可為空,並且在我的驗證中應該是唯一的。 這是我的代碼。 你能幫我解決這個問題嗎 controller
$this->validate($request, [
'class_teacher' => 'unique:sections',
]);
在遷移中
public function up()
{
Schema::create('sections', function (Blueprint $table) {
...
$table->bigInteger('class_teacher')->unsigned()->nullable();
...
$table->foreign('class_teacher')->references('id')->on('teachers');
});
}
在刀片
<select class="select2" name="class_teacher" id="teacher">
<option value=""></option>
@foreach($teachers as $teacher)
<option value="{{ $teacher->id }}" {{ old('class_teacher') == $teacher->id ? 'selected' : '' }}>{{ $teacher->name }}</option>
@endforeach
</select>
這里 class_teacher 是教師表中的外鍵。 但我想為 class 老師保持可空性和唯一性。 但現在在驗證中。 它總是發送錯誤。 The class teacher has already been taken.
任何人都可以幫助我保持這個驗證,讓它保持唯一並且可以為空嗎? 請?
以這種方式更改您的驗證規則
$this->validate($request, [
'class_teacher' => 'sometimes|nullable|unique:sections',
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.