Laravel Eloquent独特的验证问题

时间:2016-03-15 14:59:34

标签: php validation laravel laravel-5 eloquent

我目前正在尝试为公司表中的customer_no添加验证。

public function rules()
{
    $id = Company::where('customer_no', '=', $this->get('customer_no'))->first();
    $id = $id['attributes']['id'];

    return [
        'customer_no' => 'required|unique:companies,customer_no,'.$id,
    ];

}

添加一个已存在customer_no的新公司后,我从唯一数据库迁移中收到以下错误消息。

SQLSTATE[23000]: Integrity constraint violation: 
1062 Duplicate entry 'abc' for key 'companies_customer_no_unique' 
(SQL: update `companies` set `customer_no` = abc, `updated_at` = 2016-03-15 14:50:28 where `id` = 1)

但它应该将我重定向到上一页并显示错误消息。这已经适用于必填字段,变量$ id包含公司的ID(使用var_dump检查)。

编辑:( companyController存储)

    /**
 * Store a newly created resource in storage.
 *
 * @param  ManageCompanyRequest  $request
 * @return Response
 */
public function store(ManageCompanyRequest $request)
{

    $company = $request->all();

    if(!(CompanyType::where('id','=',$request->company_type_id)->exists()  || CompanyType::where('name', 'like', $request->company_type_id)->exists()))
        $company['company_type_id'] = CompanyType::create(['name' => $request->company_type_id])->id;
    else if(CompanyType::where('name', 'like', $request->company_type_id)->exists())
        $company['company_type_id'] =  CompanyType::where('name', 'like', $request->company_type_id)->first(['id'])->id;
    $address = Address::create($request->all());

    $company['address_id'] = $address->id;

    Company::create($company);

    return redirect('companies');
}

CompanyController更新:

    /**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return Response
 */
public function update(ManageCompanyRequest $request, $id)
{
    if(!(CompanyType::where('id','=',$request->company_type_id)->exists()  || CompanyType::where('name', 'like', $request->company_type_id)->exists()))
        $request->company_type_id = CompanyType::create(['name' => $request->company_type_id])->id;
    else if(CompanyType::where('name', 'like', $request->company_type_id)->exists())
        $request->company_type_id =  CompanyType::where('name', 'like', $request->company_type_id)->first(['id'])->id;

    $company = Company::find($id);
    Company::find($id)->update([
        'company_type_id'   => $request->company_type_id,
        'customer_no'       => $request->customer_no,
        'name'              => $request->name,
        'comment'           => $request->comment
    ]);


   Address::where('id', '=', $company->address_id)->update([
        'country_code_id'   => $request->country_code_id,
        'city'          => $request->city,
        'region'            => $request->region,
        'postal_code'       => $request->postal_code,
        'street'           => $request->street
    ]);


    return redirect('companies');
}

1 个答案:

答案 0 :(得分:2)

如果您希望控制器将您重定向回表单,则需要更新验证。当您检测到数据库中的冲突时(即customer_no不唯一),您应该执行以下操作:

return Redirect::back()->withInput()->withErrors(['Error message here']);

E.g。

if(!(CompanyType::where('id','=',$request->company_type_id)->exists()  || CompanyType::where('name', 'like', $request->company_type_id)->exists()))
        return Redirect::back()->withInput()->withErrors(['Details are not unique']);