0

in the code below I call a function in another Controller. Is it a good way to do that as I did?

public function result(Request $request)
{
    $request->validate([
        'username' => [
            'required', 'alpha_num', new ExistingUser, new UserNotAdmin
        ]
    ]);

    $username = $request->username;
    $user = User::where('name', $username)->select('id')->first();

    return (new InvitationController)->show($user->id);
}
  • Possible duplicate of [Access Controller method from another controller in Laravel 5](https://stackoverflow.com/questions/30365169/access-controller-method-from-another-controller-in-laravel-5) – Sauav Oct 03 '18 at 11:15

2 Answers2

0

Instead you can redirect it by using redirect action corresponding to show method in InvitationController

redirect()->action(
    'InvitationController@show', ['id' => $user->id]
);

for more details refer https://laravel.com/docs/5.7/redirects#redirecting-controller-actions

Or you can use

app(InvitationController::class')->show($user->id)

directly in the controller to access other controller's functions

Jayashree
  • 153
  • 1
  • 6
  • I used this before but I don't want to use a route. I want directly run the function. –  Oct 03 '18 at 11:41
0

yes this is better way to call function in controller without using laravel any method for create instance of InvitationController.

return (new InvitationController)->show($user->id); 
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57