在 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');
}
任何人都知道这里发生了什么?