Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理laravel中的MySQL错误_Laravel - Fatal编程技术网

如何处理laravel中的MySQL错误

如何处理laravel中的MySQL错误,laravel,Laravel,在我的应用程序中有一个如下表单,并且有一个email字段,数据从该字段转到数据库表中的email unique列。 问题是,当我输入重复数据时,它将显示以下错误页面,而我希望在电子邮件的表单字段中显示“重复输入” 在提交insert sql之前,如果返回值与否,可以使用select查询和计数。如果返回值,您可以让系统显示错误,否则将数据插入数据库 或者您可以使用try-catch方法来避免错误发生您可以使用laravel的方法。发布表单时,在控制器中: <?php public fu

在我的应用程序中有一个如下表单,并且有一个email字段,数据从该字段转到数据库表中的email unique列。

问题是,当我输入重复数据时,它将显示以下错误页面,而我希望在电子邮件的表单字段中显示“重复输入”


在提交insert sql之前,如果返回值与否,可以使用select查询和计数。如果返回值,您可以让系统显示错误,否则将数据插入数据库

或者您可以使用try-catch方法来避免错误发生

您可以使用laravel的方法。发布表单时,在控制器中:

<?php 

public function store(Request $request)
{
    // Assuming that the table name is users and the column name of email field is email_address
    $request->validate([
        'email' => 'email|unique:users,email_address',
        'your_other_fields' => 'validationRules...'
    ]);

    // Your store logic here

}

首先,将代码放在这里,而不是图像。你检查过输入和验证了吗?非常感谢,这是工作。
<div class="form-group">
    <label>Email *</label>
    <input class="form-control" placeholder="enter email" name="email">
    @if ($errors->has('email'))
        <span class="invalid-feedback" role="alert">
            <strong>{{ $errors->first('email') }}</strong>
        </span>
    @endif
</div>