【问题标题】:Laravel 5 Request ClassLaravel 5 请求类
【发布时间】:2015-11-03 20:59:06
【问题描述】:

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

有人知道这里发生了什么吗?

【问题讨论】:

  • update()的开头添加dd($request-&gt;all())并在此处发布输出
  • 我仍然收到相同的错误消息,因为 $request 是从有错误的 ClientRequest 类获得的!

标签: php mysql laravel-5 laravel-request


【解决方案1】:

在编辑记录的情况下,您需要在规则集中传递当前记录 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;
}
}

【讨论】:

  • 我知道我们曾经在 Laravel 4.2 中这样做过,但在 Laravel 5 中情况发生了变化。我不想有两个请求类来创建和更新模型。
  • 您不必创建两个类对象来处理这个问题,您可以根据请求方法在同一个请求类中定义不同的规则集,例如如果它的 POST 方法然后定义添加规则的规则记录,如果它的 PATCH 方法则规则编辑记录。
  • 我认为这个 if 语句 if($clients = $this-&gt;clients){ ... } 解决了这个问题。我怎样才能使用你的方法?请写一些示例代码
  • 更新了我的答案,添加了基于请求方法添加规则的示例代码
  • 这部分$user-&gt;id给了我一个错误信息Trying to get property of non-object
【解决方案2】:

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

    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 ;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2017-11-15
    • 1970-01-01
    • 2023-03-14
    • 2015-07-06
    • 2015-04-20
    • 2015-08-07
    相关资源
    最近更新 更多