skip to Main Content

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 AppHttpControllersSubscriberController::subscribe(), 0 passed in C:xampphtdocs[folder]vendorlaravelframeworksrcIlluminateRoutingControllerDispatcher.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 AppHttpControllers;

use AppHttpRequestsSubscriberRequest;
use AppRepositoriesSubscriberRepository;
use IlluminateHttpRequest;

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(); 
});

SubscriberRepo:

<?php

namespace AppRepositories;

use AppModelsSubscriber;

class SubscriberRepository
{
    public function newOrUpdate($data)
    {
        dd($data);
        Subscriber::updateOrCreate([
            'email' => $data->email
        ], $data);
    }
}

And finally the request which is currently commented out:

<?php

namespace AppHttpRequests;

use IlluminateFoundationHttpFormRequest;

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, IlluminateContractsValidationValidationRule|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

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

2

Answers


  1. 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
    }
    
    Login or Signup to reply.
  2. 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());
        // ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search