0

I'm developing a small application in Laravel 5.5 where I'm creating a request with updateContact and having unique email validation rule, while using the same validation inside the controller I can easily make:

$contact = Contact::find($request->id);
Validator::make($data, [
    'first_name' => 'required|max:255',
    'email' => 'required', Rule::unique('contacts')->ignore($contact->id),
    'address' => 'max:255',
    'city' => 'max:255',
    'state' => 'max:255',
    'country' => 'max:255',
]);

and can validate, by I made out request through php artisan make:request UpdateContact and added the following inside updateContact.php:

namespace App\Http\Requests;

use App\Contact;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateContact 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()
    {
        $contact = Contact::find($request->id);
        return [
            'first_name' => 'required|max:255',
            'email' => 'required', Rule::unique('contacts')->ignore($contact->id),
            'company_id' => 'required',
            'address' => 'max:255',
            'city' => 'max:255',
            'state' => 'max:255',
            'country' => 'max:255',
        ];
    }

    public function messages()
    {
        return [
            'company_id.required' => 'Company name is required'
        ];
    }
}

But I don't know about the $request->id how can I use this?

Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
  • I'm not sure, but you could inject it as an attribute/parameter in the `rules()` method. Try `public function rules(Request $request)`. Then you could make `$request->id` – Kenny Horna Jan 26 '18 at 03:00
  • @HCK Laravel request itself is a `Request` as we take `use Illuminate\Foundation\Http\FormRequest;` this is the confusing part whether to inject request again inside this? – Nitish Kumar Jan 26 '18 at 03:04
  • you can access the current request using the [request](https://laravel.com/docs/5.5/helpers#method-request) helper – Amr Aly Jan 26 '18 at 03:07
  • Check this answer: https://stackoverflow.com/a/28530960/7117697 – Kenny Horna Jan 26 '18 at 03:10

3 Answers3

2

Inside a FormRequest class, $this is the variable scope of the request object. Simply do the following to get the ID property

$contact = Contact::find($this->id);

Same goes for any other field in your form.

Rob Fonseca
  • 3,671
  • 2
  • 17
  • 13
2

Why you are finding Contact with id again that you already have in Request object ?

You can simply use this code:

 'email' => 'required', Rule::unique('contacts')->ignore($this->id),

You are using id to find Contact and then again using $contact->id that you already have in Request object that is $this. You can simply use $this->id it will give you the id of the Contact. There is no need to write below line:

 $contact = Contact::find($this->id);

because

$this->id is same as $contact->id

You can also validate unique email using below code:

'email' => "required|unique:users,email,$this->id,id" //use double quotation marks

It will do the same as:

'email' => 'required', Rule::unique('contacts')->ignore($this->id),

Have a look at snippet that use in Request class

public function rules()
{
    switch ($this->method()) {
        case 'POST':
        {
            return [
                'name' => 'required',
                'email' => 'required|email|unique:users',
                'password' => 'required'
            ];
        }
        case 'PUT':
        case 'PATCH':
        {
            return [
                'name' => 'required',
                'email' => "required|unique:users,email,$this->id,id"
                 //OR you can use below rule
                // 'email' => Rule::unique('users')->ignore($this->id),
            ];
        }
        default: break;
    }
}
Afraz Ahmad
  • 5,193
  • 28
  • 38
-1

To call object of the class within it you can use $this variable scope. and then access any variable that is part of the object. e.g $this->id, $this->name etc.

If you want to access static function within same class then you can use self keyword. for example you have a static function

public static function add($a, $b){
    return $a+$b;
}

you call add method like self::add(2,2);

Afraz Ahmad
  • 5,193
  • 28
  • 38