Php SQLSTATE[23000]:完整性约束冲突:1048 Le champ';url';ne peut和#xEA;树视频(空)

Php SQLSTATE[23000]:完整性约束冲突:1048 Le champ';url';ne peut和#xEA;树视频(空),php,mysql,database,laravel,Php,Mysql,Database,Laravel,我正在学习Laravel和PHP,当我提交一个空字段时,我收到这个错误: SQLSTATE[23000]:完整性约束冲突:1048 Le champ'url' ne peutêtre vide(null)(SQL:插入url(url,缩写) 数值(?,6kuHSS)) 我迷路了,我不明白是哪个代码导致了这个错误 这是我的密码: Route::post('/', function(){ function make_shortned(){ $shortned = Str::r

我正在学习Laravel和PHP,当我提交一个空字段时,我收到这个错误:

SQLSTATE[23000]:完整性约束冲突:1048 Le champ'url' ne peutêtre vide(null)(SQL:插入
url
url
缩写
) 数值(?,6kuHSS))

我迷路了,我不明白是哪个代码导致了这个错误

这是我的密码:

Route::post('/', function(){
    function make_shortned(){
        $shortned = Str::random(6);
        if(App\Url::whereShortned($shortned)->first())
        {
            return make_shortned();
        }
        else
        {
            return $shortned;
        }
    }

    $data = ['url' => request('url')];
    $validation = Validator::make($data, ['url' => 'required|url']);


    $url = App\Url::where('url', request('url'))->first();

    if($url) // le short existe déjà en bdd
    {
        return view('result')->withShortned($url->shortned);
    }
    else
    {
        $row = App\Url::create([
            'url' => request('url'),
            'shortned' => make_shortned()
        ]);

        if($row) // L'entrée a bien été créée 
        {
            return view('result')->withShortned($row->shortned);
        }
        else
        {
            echo 'test';
        }

    }
});

如果
url
字段不能为空,则需要在验证中检查该字段。您当前正在创建验证程序实例,但未实际验证:

Route::post('/', function(Request $request){
    function make_shortned() {
        $shortned = Str::random(6);
        if(App\Url::whereShortned($shortned)->first())
        {
            return make_shortned();
        }
        else
        {
            return $shortned;
        }
    }

    $data = $request->validate([
        'url' => 'required|url',
    ]);


    $url = App\Url::where('url', $request->input('url'))->first();

    if($url) 
    {
        return view('result')->withShortned($url->shortned);
    }
    else
    {
        $row = App\Url::create([
            'url' => $request->input('url'),
            'shortned' => make_shortned()
        ]);

        if($row) // L'entrée a bien été créée 
        {
            return view('result')->withShortned($row->shortned);
        }
        else
        {
            echo 'test';
        }

    }
});

您必须将url设置为可空更新或创建新迁移

Schema::table('urls', function (Blueprint $table) {$table->string('url', 225)->after('id')->nullable(); });

您是否在模型中添加了$fillable<代码>受保护的$fillable=['url','shortned']我想是的,这是我的Url.php模型:
Url
列可以为空吗?能否显示dd(request())?好的,我是这样做的:$table->string('url')->nullable()->change();然后php artisan迁移谢谢,但现在我可以看到我的验证器规则不起作用ErrorException未定义变量:Request注意route函数中的
Request$Request