title: Laravel: Passing parameters in FormRequest & Custom Rule
body: <p>I am trying to pass the <code>$this-&gt;repo</code> that I have initiated in controller to be available in Custom rule class <code>EmployeeWorkingHours</code> which I am calling using <code>EmployeeRequest</code>, so that I can fetch the data from database for validations.</p>
  
  <p>Below is my code sample:</p>
  
  <p>Controller:</p>
  
  <pre><code>&lt;?php
  
  namespace Modules\ShopManager\Http\Controllers;
  
  use App\Http\Controllers\Controller;
  use Auth;
  use Illuminate\Http\Request;
  use Illuminate\Http\Response;
  use Modules\ShopManager\Entities\Repository\Contract\ShopManagerRepository;
  use Validator;
  use Modules\ShopManager\Http\Requests\EmployeeRequest;
  
  class EmployeesController extends Controller
  {
      public function __construct(ShopManagerRepository $repo)
      {
          $this-&gt;repo = $repo;
  
          $this-&gt;middleware(function ($request, $next) {
  
              $this-&gt;repo-&gt;setManagerId(Auth::id()); //new $repo(Auth::id());
              $this-&gt;shopId = $this-&gt;repo-&gt;getShopId();
  
              if (!is_null($this-&gt;shopId)) {
                  $this-&gt;storagePath = 'images/shopmanager/shop/' . $this-&gt;shopId . '/employees';
              } else {
                  abort(404);
              }
  
              return $next($request);
          });
      }
  
      public function store(EmployeeRequest $request)
      {           
  
          $data = $request-&gt;all();
          ...
          ...
      }
  }
  </code></pre>
  
  <p>EmployeeRequest:</p>
  
  <pre><code>&lt;?php
  
  namespace Modules\ShopManager\Http\Requests;
  
  use Illuminate\Foundation\Http\FormRequest;
  use Modules\ShopManager\Rules\EmployeeWorkingHours;
  
  class EmployeeRequest extends FormRequest
  {
      /**
       * Get the validation rules that apply to the request.
       *
       * @return array
       */
      public function rules()
      {
          return [
              'first_name' =&gt; 'required',
              'last_name' =&gt; 'required',
              'email' =&gt; 'required|email',
              'from_hour' =&gt; ['required','numeric', new EmployeeWorkingHours],
              'to_hour' =&gt; ['required','numeric', new EmployeeWorkingHours],
              'status' =&gt; 'required|numeric',
              'profile_image' =&gt; 'image',
          ];
      }
  
      public function messages()
      {
          return [
              'profile_image.image' =&gt; 'Uploaded file is not an image.',
          ];
      }
      public function attributes()
      {
          return [
              'first_name' =&gt; 'First Name',
              'last_name' =&gt; 'Last Name',
              'email' =&gt; 'Email'
          ];
      }
      /**
       * Determine if the user is authorized to make this request.
       *
       * @return bool
       */
      public function authorize()
      {
          return true;
      }
  }
  </code></pre>
  
  <p>EmployeeWorkingHours: Rule</p>
  
  <pre><code>&lt;?php
  
  namespace Modules\ShopManager\Rules;
  
  use Illuminate\Contracts\Validation\Rule;
  
  class EmployeeWorkingHours implements Rule
  {
      private static $from;
      private static $to;
      /**
       * Create a new rule instance.
       *
       * @return void
       */
      public function __construct()
      {
          //
      }
  
      /**
       * Determine if the validation rule passes.
       *
       * @param  string  $attribute
       * @param  mixed  $value
       * @return bool
       */
      public function passes($attribute, $value)
      {
          switch ($attribute) {
              case 'from_hour':
                  self::$from = $value;
                  break;
              case 'to_hour':
                  self::$to = $value;
                  return $this-&gt;_validate();
                  break;
          }        
      }
  
      /**
       * Get the validation error message.
       *
       * @return string
       */
      public function message()
      {
          return 'Please select valid Time Interval From hour &amp; To hour.';
      }
  
      private function _validate(){
          //dd(self::$from, self::$to);
          return false;
      }
  }
  </code></pre>