Laravel Nullable не работает, я пробую все, но все равно не работает. Пожалуйста, посмотрите мой код ..
Контроллер: -
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Pop;
class PopController extends Controller
{
public function index(){
return view('test2');
}
public function create(Request $request){
$formvalidtion = $request->validate([
'usercode' => ['nullable', 'required'],
]);
return "<h1>SUCCESS</h1>";
}
}
Модель: -
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pop extends Model
{
protected $timestamps = false;
protected $fillable = ['usercode'];
}
Миграция: -
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePopsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pops', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('usercode')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('pops');
}
}
Маршрут: -
route::get('test', 'PopController@index');
route::post('testcheck', 'PopController@create')->name('uc');
Просмотр: -
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Nullable Test</title>
</head>
<body>
<h1>Test Nullable</h1>
<form action="{{ route('uc') }}" method="POST">
@csrf
<input type="number" name="usercode" id="" placeholder="Enter User Code">
@error('usercode')
{{ $message }}
@enderror
<br>
<input type="submit" >
</form>
</body>
</html>
Я трачу 1 час, чтобы найти решение, но не получить. В некоторых видео на YouTube упоминается, что тип данных поля должен быть int и иметь значение null. это также не работает, есть ли какое-либо решение?