Laravel FormRequest fetch parameter from url

435 views Asked by At

In my update method, I am using FormRequest to validate the input fields. I also need to pass and additional parameter for unique validation rule.

I am trying to fetch the $subscriber variable from the url.

SubscriberController.php

class SubscriberController extends Controller
{
  public function update(UpdateSubscriberRequest $request, $subscriber)
  {
    ...
  }
}

UpdateSubscriberRequest.php

class UpdateSubscriberRequest extends FormRequest
{
  private $data       = [];
  private $subscriber = null;

  public function __construct()
  {
    $this->data       = request()->all();
    $this->subscriber = request()->route('subscriber');
  }

  public function rules()
  {
    return [
      ...

      'email'  => [
        'required', 
        ...
        "unique:subscribers,email,{$this->subscriber},token,company_id," 
          . session()->get('COMPANY_ID')
      ],
      'mobile' => [
        'required', 
        ...
        "unique:subscribers,mobile,{$this->subscriber},token,company_id,"
          . session()->get('COMPANY_ID')
      ],

      ...
    ];
  }
}

Although the code is working fine. But, is this the right way to do this..?

0

There are 0 answers