2016-03-15 45 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

是的,那也可以。但由於我沒有看到你使用驗證,我雖然你想要一個解決方法。 –