为什么会发生重复?

时间:2019-06-18 09:46:49

标签: php laravel

  

SQLSTATE [23000]:违反完整性约束:1062项“ students_email_unique”的重复项“ admin@gmail.com”(SQL:插入到studentsnameaddressphoneemailfacultyupdated_atcreated_at)值(图书管理员,maitidevi,9738232331,admin @ gmail.com,食品,2019年6月- 18 09:38:58,2019-06-18 09:38:58))   以前的例外情况

public function update(Request $request, $id)
{
    $request->validate([

        'name' => 'required',
        'address' => 'required',
        'phone' => 'required',
        'email' => 'required|unique',
        'faculty' => 'required' 
    ]);


    Student::create($request->all());
    return redirect()->route('student.index')
    ->with('success', 'Student Updated Successfully');
}

当我用唯一性验证时,这也会发生: 唯一的验证规则至少需要1个参数。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Student;

class StudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $students = Student::latest()->paginate(5);
        return view('student.index', compact('students'))
                  ->with('i', (request()->input('page', 1) -1)*5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('student.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([

            'name' => 'required',
            'address' => 'required',
            'phone' => 'required',
            'email' => 'required',
            'faculty' => 'required'
        ]);


        Student::create($request->all());
        return redirect()->route('student.index')
        ->with('success', 'Student Created Successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
       $student = Student::find($id);
       return view('student.detail', compact('student'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       $student = Student::find($id);
        return view('student.edit', compact('student'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([

            'name' => 'required',
            'address' => 'required',
            'phone' => 'required',
            'email' => 'required',
            'faculty' => 'required'
        ]);


        Student::create($request->all());
        return redirect()->route('student.index')
        ->with('success', 'Student Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $student = Student::find($id);
        $student->delete();
        return redirect()->route('student.index')
                        ->with('success', 'Student deleted successfully');
    }
}

我被卡在上面的代码中了吗?请帮忙怎么办?

1 个答案:

答案 0 :(得分:3)

store方法的验证中,将其替换为

'email' => 'required|unique:students,email',

在Update方法中,您将需要构建像这样的验证规则

use Illuminate\Validation\Rule;
/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    \Validator::make($request->all(), [
        'name' => 'required',
        'address' => 'required',
        'phone' => 'required',
        'faculty' => 'required'
        'email' => [
            'required',
            Rule::unique('students', 'email')->ignore($id),
        ],
    ]);

    if ($validator->fails()) {
        return redirect()
                ->route('student.create')
                ->withErrors($validator)
                ->withInput();
    }

    $student = Student::findOrFail($id);
    foreach ($request->all() as $attribute => $value) {
        $student->{$attribute} = $value;
    }
    $student->save();
    return redirect()->route('student.index')
    ->with('success', 'Student Updated Successfully');

}