0

在 Laracasts,特别是 Laravel 5 Fundamentals 中,Jeffrey 提到使用相同的请求类来创建和更新模型。我已经尝试过了,但出现错误:

这是我的请求类

<?php namespace CRM\Http\Requests;

use CRM\Http\Requests\Request;

class ClientRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public $rules = [
            'company'   => 'required|min:3',
            'firstname' => 'required|min:3',
            'lastname'  => 'required|min:3',
            'email'     => 'required|email|unique:clients,email',
            'phone'     => 'required|min:6|phone|unique:clients,phone',
            'address'   => 'required|min:3'
        ];

    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if($clients = $this->clients){

            $this->rules['email'] .= ','.$clients ;
            $this->rules['phone'] .= ','.$clients ;
        }

        return $this->rules;
    }

    public function messages()
    {
        return ['phone' => 'The phone number is not valid. Please confirm and try again.'];
    }

}

当我创建新记录时它工作正常,但当我更新记录时抛出错误。

这是错误消息

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'company:"BWA"' in 'where clause' (SQL: select count(*) as aggregate from `clients` where `email` = support@example.co.ke and `company:"BWA"` <> {"id":1 and `firstname:"richard"` = lastname:"keep" and `email:"support@example`.`co`.`ke"` = phone:"+27521341661" and `address:"test address"` = deleted_at:null and `created_at:"2015-04-15 08:46:45"` = updated_at:"2015-04-15 09:24:55"})

这是我的控制器方法

   /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Client $client, ClientRequest $request)
    {
        $client->update($request->all());

        return redirect('clients')->with('success', 'Client Updated Successfully');
    }

任何人都知道这里发生了什么?

4

2 回答 2

0

在编辑记录的情况下,您需要在一组规则中传递当前记录 id 以实现唯一性。对于您的情况,编辑记录的规则可以这样写

public $rules = [
        'company'   => 'required|min:3',
        'firstname' => 'required|min:3',
        'lastname'  => 'required|min:3',
        'email'     => 'required|email|unique:clients,email',
        'phone'     => 'required|min:6|phone|unique:clients,phone,id',
        'address'   => 'required|min:3'
];

此外,您还必须为编辑和插入案例编写单独的规则。

处理同一请求类中的多个规则的代码示例

public function rules()
{
$user = User::find($this->users);
switch($this->method())
{
    case 'GET':
    case 'DELETE':
    {
        return [];
    }
    case 'POST':
    {
        return [
            'user.name.first' => 'required',
            'user.name.last'  => 'required',
            'user.email'      => 'required|email|unique:users,email',
            'user.password'   => 'required|confirmed',
        ];
    }
    case 'PUT':
    case 'PATCH':
    {
        return [
            'user.name.first' => 'required',
            'user.name.last'  => 'required',
            'user.email'      => 'required|email|unique:users,email,'.$user->id,
            'user.password'   => 'required|confirmed',
        ];
    }
    default:break;
}
}
于 2015-04-15T11:12:26.033 回答
0

我已经设法解决了。我换了

    if($clients = $this->clients){

        $this->rules['email'] .= ','.$clients ;
        $this->rules['phone'] .= ','.$clients ;
    }

if($this->method == "PUT"){
       $this->rules['email'] .= ','.$this->clients->id ;
       $this->rules['phone'] .= ','.$this->clients->id ;
}
于 2015-04-15T12:10:10.273 回答