I am attempting to post a form post request using Laravel 11 and PHP 8.3 however the post request is missing from my parameters.
I am getting the error:
Too few arguments to function
App\Http\Controllers\SubscriberController::subscribe(), 0 passed in C:\xampp\htdocs[folder]\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php on line 46 and exactly 1 required
My web routes include:
Route::post('subscribe', [SubscriberController::class, 'subscribe']);
I have been doing some debug on here but the code currently stands at:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\SubscriberRequest;
use App\Repositories\SubscriberRepository;
use Illuminate\Http\Request;
class SubscriberController extends Controller
{
private $subscriberRepository;
public function __construct(SubscriberRepository $subscriberRepository) {
$this->subscriberRepository = new SubscriberRepository();
}
public function subscribe($request){//)SubscriberRequest $request) {
dd($request);
$request->telno = $this->formatTelNo($request->telno);
$subscriber = $this->subscriberRepository->newOrUpdate($request);
return redirect()->back()->with('success', 'You have successfully subscribed for updates.');
}
}
I have included the CSRF Token which I am currently sneakily obtaining via a web route:
Route::get('/token', function () {
return csrf_token();
});
And finally the request which is currently commented out:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SubscriberRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
'telno' => 'required|string|minlength:10|maxlength:20',
];
}
}
Can anyone explain to me why the post request is not being sent to the subscribe parameters?
As far as I can work out the post request is supposed to be injected into all post route, no?
Really pulling my hair out on this as I feel like it should be working lol
I have updated the code in the controller to the following as per the requests in comments:
public function subscribe(SubscriberRequest $request) {
dump("Hello");
dd($request->validated());
$request->telno = $this->formatTelNo($request->telno);
$subscriber = $this->subscriberRepository->newOrUpdate($request);
return redirect()->back()->with('success', 'You have successfully subscribed for updates.');
}
Image if postman post request
As you can see the post request is not being passed I am simply returning the token if I use the SubscriberRequest codehinting or a parameter error if I do not code hint
I have subsequently tried to just codehint a Request $request
and dump("Hello");
and dd($request->validated());
and I get the following
Request Output
This is making me think there is an issue with my request somewhere but I cannot find it
Any help is appeciated
What I was expecting to happen:
the code to run and the $request data to be dd-ed on screen so I can continue my debug and insert the data into subscribers and subsequently send an email to all subscribers and other form submissions
I am attempting to post a form post request using Laravel 11 and PHP 8.3 however the post request is missing from my parameters.
I am getting the error:
Too few arguments to function
App\Http\Controllers\SubscriberController::subscribe(), 0 passed in C:\xampp\htdocs[folder]\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php on line 46 and exactly 1 required
My web routes include:
Route::post('subscribe', [SubscriberController::class, 'subscribe']);
I have been doing some debug on here but the code currently stands at:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\SubscriberRequest;
use App\Repositories\SubscriberRepository;
use Illuminate\Http\Request;
class SubscriberController extends Controller
{
private $subscriberRepository;
public function __construct(SubscriberRepository $subscriberRepository) {
$this->subscriberRepository = new SubscriberRepository();
}
public function subscribe($request){//)SubscriberRequest $request) {
dd($request);
$request->telno = $this->formatTelNo($request->telno);
$subscriber = $this->subscriberRepository->newOrUpdate($request);
return redirect()->back()->with('success', 'You have successfully subscribed for updates.');
}
}
I have included the CSRF Token which I am currently sneakily obtaining via a web route:
Route::get('/token', function () {
return csrf_token();
});
And finally the request which is currently commented out:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SubscriberRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
'telno' => 'required|string|minlength:10|maxlength:20',
];
}
}
Can anyone explain to me why the post request is not being sent to the subscribe parameters?
As far as I can work out the post request is supposed to be injected into all post route, no?
Really pulling my hair out on this as I feel like it should be working lol
I have updated the code in the controller to the following as per the requests in comments:
public function subscribe(SubscriberRequest $request) {
dump("Hello");
dd($request->validated());
$request->telno = $this->formatTelNo($request->telno);
$subscriber = $this->subscriberRepository->newOrUpdate($request);
return redirect()->back()->with('success', 'You have successfully subscribed for updates.');
}
Image if postman post request
As you can see the post request is not being passed I am simply returning the token if I use the SubscriberRequest codehinting or a parameter error if I do not code hint
I have subsequently tried to just codehint a Request $request
and dump("Hello");
and dd($request->validated());
and I get the following
Request Output
This is making me think there is an issue with my request somewhere but I cannot find it
Any help is appeciated
What I was expecting to happen:
the code to run and the $request data to be dd-ed on screen so I can continue my debug and insert the data into subscribers and subsequently send an email to all subscribers and other form submissions
Share edited Jan 26 at 19:28 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 5 at 18:45 cvmflinchycvmflinchy 11 bronze badge 7 | Show 2 more comments2 Answers
Reset to default 0Your controller method is missing the instance of SubscriberRequest
. You should rewrite your code to this.
public function subscribe(SubscriberRequest $request) {
dd($request->validated()); // returns all the validated data as an array
}
You have a missing dependency SubscriberRequest about your form request. Just follow below code.
public function subscribe(SubscriberRequest $request)
{
// for only validated request data
dd($request->validated());
// for all form request data
dd($request->all());
// ...
}
SubscriberRequest
out? In the absence of any type hints Laravel will attempt to fill in function parameters from the request path and your route oath doesn't accept any parameters – apokryfos Commented Jan 5 at 19:39