【问题标题】:Laravel Eloquent Unique validation issueLaravel Eloquent Unique 验证问题
【发布时间】:2016-07-01 02:28:58
【问题描述】:

我目前正在尝试在公司表中为 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');
}

【问题讨论】:

  • 您可以尝试在 Controller 的方法中添加 try catch 语句。这将防止代码产生如此丑陋的前端错误。你能在代码中添加控制器方法吗?
  • 是否需要将 ID 提供给验证规则数组?我认为只指定 'customer_no' => 'required|unique:companies' 就足够了。您对该规则所做的实际上是强制验证器忽略提供的 ID。请参阅“强制使用唯一规则以忽略给定 ID”:laravel.com/docs/master/validation
  • 编辑现有公司需要ID。为了更新条目,我必须忽略此特定条目的 customer_no。它适用于没有除外部分的商店,但不适用于更新。新增 companyController 的 store() 和 update()。

标签: php validation laravel laravel-5 eloquent


【解决方案1】:

如果您希望控制器将您重定向回表单,您需要更新验证。当您在数据库中检测到冲突时(即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']);

【讨论】:

  • 这是一个很好的解决我的问题的方法,我真的很感激。无论如何,我希望验证规则:unique:companies,customer_no,'.$id, 完全一样。将是让整个验证工作的一种更清洁的方法。
  • 是的,这也行。但由于我没有看到您使用 Validation,因此我认为您需要一种解决方法。
猜你喜欢
  • 2019-02-07
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多