Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BenSmith's avatar

[4.3] How to validate route parameters within request objects

I'm currently porting over Larabook to 4.3 (I'll post it here once complete) and I've hit a speed bump. I'm in the process of adding the unfollow user functionality and my unfollow form sends a delete request to follows/{followed_id} where followed id is the id of the user to unfollow.

Now, within my request object I would like to like to add a validation rule of 'exists:users,id' (no point in making it required as it must be present for the form to submit). One way to do this would just be to add the followed_id variable as a hidden field in the unfollow form. Whilst easy enough to do, it seems a bit strange given that we have access to this value in the request object through $request->route()->parameter('followed_id'). Is this the suggested approach or is there another way that I am overlooking.

0 likes
2 replies
JordanDalton's avatar

I hope this helps you.

<?php namespace App\Http\Requests\Auth;

use Illuminate\Foundation\Http\FormRequest;

class UnfollowRequest extends FormRequest {

   /**
    * Get the validation rules that apply to the request.
    *
    * @return array
    */
   public function rules()
   {
      // Here's how you could access it from within.
      // 
      $id = $this->route->parameter('followed_id');

      return [
       'followed_id' => 'exists:users,id',
      ];
   }

   /**
    * Determine if the user is authorized to make this request.
    *
    * @return bool
    */
   public function authorize()
   {
    return true;
   }

}
1 like
pmall's avatar

Yes but sometimes you need more data related to this id. So I guess you inject a repository in the rules method and retrieve the related model, create proper validation rules, retrieve the model one more time in the authorize method, retrieve the model again in the controller method, and it start to feel very weird, haha.

Please or to participate in this conversation.