最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - Why is my request parameter missing from the post request? - Stack Overflow

programmeradmin6浏览0评论

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
  • What happens when you inject Request $request instead of just $request on the subscribe method? Do you get any response when you dd $request? – Olumuyiwa Commented Jan 5 at 19:11
  • Also, there is no point sharing the SubscribeRepo because as the error says, it has nothing to do with that but can't find an argument for the subscribe method where 1 argument is expected. It would be easier if you shared your html code for the form where the post request is being made. – Olumuyiwa Commented Jan 5 at 19:27
  • 1 Why did you comment 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
  • When injecting Request $request I successfully get an output of dd($request) but not of ->validated() – cvmflinchy Commented Jan 8 at 11:38
  • I commented the SubscriberRequest out to see if I could do some debugging to see if there was an issue with my post request. This code is supposed to be (and is) commented in. It was only commented out for debug purposes – cvmflinchy Commented Jan 8 at 11:39
 |  Show 2 more comments

2 Answers 2

Reset to default 0

Your 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());
    // ...
}
发布评论

评论列表(0)

  1. 暂无评论