Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call controller with parameter

I try to call a controller in an other controller with a parameter. I have no problem without any parameters.

return app()->call('App\Http\Controllers\AppointmentController@create');

AppointmentController.php

class AppointmentController extends Controller
{

    public function create(CalendarInterface $calendar, Request $request) {
        ...
    }
}

But I have an error when I try to pass one.

return app()->call('App\Http\Controllers\AppointmentController@create', $response);

AppointmentController.php

class AppointmentController extends Controller
{

    public function create($response, CalendarInterface $calendar, Request $request) {
        ...
    }
}

Type error: Argument 2 passed to Illuminate\Container\Container::call() must be of the type array, object given

Or if I do

return app()->call('App\Http\Controllers\AppointmentController@create', [$response]);

Type error: Argument 2 passed to App\Http\Controllers\AppointmentController::create() must implement interface App\CalendarInterface, instance of Illuminate\Http\Request given in [...]/app/Http/Controllers/AppointmentController.php:18

like image 992
Neabfi Avatar asked Sep 13 '26 09:09

Neabfi


1 Answers

Use named parameters:

app()->call('App\Http\Controllers\AppointmentController@create',  [
    "response" => $response
]);
like image 123
apokryfos Avatar answered Sep 14 '26 22:09

apokryfos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!