user3489502 user3489502 - 6 months ago 15
PHP Question

Call another function within a function and pass data

To avoid duplicating code, I'd like to create the doMore() function in my controller.

doSomething() and doSomethingElse() would do stuff and then call doMore(), and each would then return to a different view.

Is this how I should do it?

class SomeController extends BaseController
{
function doSomething()
{
// Do stuff here

return $this->doMore();

return view('view1.index')->with(['data1' => $data1]->with(['data2' => $data2])
}


function doSomethingelse()
{
// Do stuff here

return $this->doMore();

return view('view2.index')->with(['data1' => $data1]->with(['data2' => $data2])
}


function doMore()
{
// Do more stuff here

return response(with(['data1' => $data1]->with(['data2' => $data2]))
}
}


Thanks

Answer

its done like this

 $result = $this->domore($something); 
return view('view1.index')->with('result' , $result)->with('test' , $test);

you will pass something to the function domore ane results will be saved in $result then you return what you want! you cant have 2 returns

but you will have to inject it if the function is in another class

Comments