2016-03-15 110 views
1

我目前正在尝试为公司表中的customer_no添加验证。Laravel Eloquent独特的验证问题

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'); 
} 
+0

您可以尝试在Controller中的方法中添加try catch语句。这将防止代码生成这样一个丑陋的前端错误。你能够将控制器方法添加到代码? – Duikboot

+0

您是否需要将ID提供给验证规则数组?我认为只是指出'customer_no'=>'必需|唯一:公司'就足够了。你用这个规则做什么实际上是迫使验证者忽略提供的ID。请参阅“强制使用唯一规则忽略给定ID”:https://laravel.com/docs/master/validation – btl

+0

编辑现有公司需要此ID。为了更新条目,我必须忽略此特定条目的customer_no。它正在为没有除外部分的商店工作,但不适用于更新。 增加了companyController的store()和update()。 –

回答

2

如果你想控制你重定向回的形式,你需要更新验证。当你在数据库中检测到冲突(即customer_no不是唯一的),你应该做的:

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

例如

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']); 
+0

对于我的问题,这是一个很好的解决方法,我非常感谢。 反正我会期待验证规则:unique:companies,customer_no,'。$ id,要完全一样。将是一个更清洁的方式来获得整个验证工作。 –

+0

是的,那也可以。但由于我没有看到你使用验证,我虽然你想要一个解决方法。 –