0

Is it okay to send Request $request object between the functions?

My Request $request object consists of around 20 inputs. At first in 'store' function, I save about 10 inputs. Then I pass this $request object to another function where I save remaining inputs. The purpose behind second function is so that I can use this function for another route if only next 10 fields are to be stored.

I know that the $request object is very large, consuming so much memory. So it is good to send the $request object function to function when considered space complexity and time complexity?

//My Controller

class MyController extends Controller{
    public function store(Request $request){
        //save 10 inputs
        $this->saveTasks($request);
   }

   public function saveTasks(Request $request){
       //save remaining 10 inputs
   }
}

// routes/web.php

Route::post('store', 'MyController@store');
Route::post('store_tasks', 'MyController@saveTasks');

Thanks in advance :)

  • 1
    There's nothing wrong with sending `$request` as a parameter, but you're correct, it is rather large. If you know you only need to send some inputs, you could use `$request->input()`, or better yet `$request->only(["field1", "field2", ...])`. Just make sure to change your signature to match, i.e. not `Request $request`, but rather `$input`. – Tim Lewis Jan 03 '19 at 18:35
  • Thanks @TimLewis. That's better. :) – Kalpashree V. Bal Jan 06 '19 at 11:53

0 Answers0