Laravel自定义请求,忽略唯一?

时间:2017-07-11 09:17:35

标签: laravel laravel-5 eloquent

嗨,我有这样的实体请求:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class EntityRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|alpha_special|unique:businesses|min:5|max:30',
            'type' => 'required',
            'email' => 'required|email|unique:businesses',
            'address1' => 'alpha',
            'address2' => 'required|alpha_spaces',
            'town' => 'required|alpha',
            'city' => 'required|alpha',
            'postcode' => 'required|max:9',
            'telephone' => 'required|numeric|min:11',
        ];
    }
    public function messages()
    {
        return [
            'name.required' => 'Business name is required',
            'name.alpha_specials' => 'Business name can only contain letters and spaces',
            'name.unique' => 'Business name must be unique',
            'name.min' => 'Business name must contain at least 5 characters',
            'name.max' => 'Business name can only contain up to 30 characters',

            'type.required' => 'Business type is required',

            'email.required' => 'Email address is required',
            'email.email' => 'Email address must be a right format',
            'email.unique' => 'Email address must be unique',

            'address1.alpha' => 'House/Building name can only contain letters',

            'address2.required' => 'Address is required',
            'address2.alpha_spaces' => 'Address can only contain letters, numbers and spaces',

            'town.required' => 'Town is required',
            'town.alpha' => 'Town can only contain letters',

            'city.required' => 'City id required',
            'city.alpha' => 'City can only contain letters',

            'postcode.required' => 'Postcode is required',
            'postcode.max' => 'Postcode can only contain 9 characters',

            'telephone.required' => 'Telephone is required',
            'telephone.numeric' => 'Telephone can only contain numbers',
            'telephone.min' => 'Telephone must contain minimum of 11 characters',
            'telephone.max' => 'Telephone can only contain maximum of 15 characters',
    ];
    }
}

我有更新功能:

public function update(EntityRequest $request)
{
    $test =$request->input('entity_id');
    $business = Business::with('addresses')
        ->find($test);

    $user_id = Auth::id();
    $business->name = $request->input('name');
    $business->type = $request->input('type');
    $business->email = $request->input('email');
    $business->twitter_business = $request->input('twitter');
    $business->facebook_business = $request->input('facebook');
    $business->instagram_business = $request->input('instagram');
    $business->google_places = $request->input('google');
    $business->save();

    $business->addresses->firstline_address = $request->input('address1');
    $business->addresses->secondline_address = $request->input('address2');
    $business->addresses->town = $request->input('town');
    $business->addresses->city = $request->input('city');
    $business->addresses->postcode = $request->input('postcode');
    $business->addresses->telephone = $request->input('telephone');
    $business->addresses->save(); 
}

现在的问题是我希望表单执行PATCH,因此某些字段不需要更新。即企业名称是必需且唯一的,但是如果名称保持不变,我仍然希望为此特定企业指定相同名称,但不允许采用任何其他现有企业名称,因此我希望它是唯一的,但如果是business已经将名称忽略为unique。

其他问题是我想更新业务表和地址表,这就是我使用的原因,这会带来结果,但这是更新2个表的正确方法吗?

0 个答案:

没有答案