Latest

Tuesday, July 11, 2017

Laravel custom request, ignore unique?

Asked by: Przemek Wojtas


Hi I have Entity request like this:

<?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',
    ];
    }
}

And I have an update function:

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

Now the problem is that I want the form to perform PATCH, so that some of the fields don't need to be update. I.e business name is required and unique however if the name stays the same I still want to give it the same name for this specific business but don't allow to take any other existing business name so I want it to be unique, but if that business already has the name ignore the unique.

Other question is that I want to update business table and address table that's why I am using with which brings back the results but is this the right way to update 2 tables?



Source

No comments:

Post a Comment

Adbox