stephan lewis
stephan lewis

Reputation: 21

Laravel can't submit to database because too few arguments to function

I'm trying to pass an id when submitting to a database but I'm getting an error. When a user submits a review for a product I need the id to be saved. I can successfully add the review with hard coded values but when the page refreshes it throws an error:

"Too few arguments to function App\Http\Controllers\PagesController::addreview(), 1 passed in /Users/stef/Desktop/GitHub/Nue/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 2 expected".

shows the product where user can add product to cart and/or submit a review show.blade.php

{!! Form::open(['action' => 'App\Http\Controllers\PagesController@addreview', 'method' => 'POST']) !!}

web.php

Route::post('/addreview', 'App\Http\Controllers\PagesController@addreview');

saves review to database then redirects back to the product page pagescontroller.php

    public function addreview(Request $request, $id)
    {

        $this->validate($request, [

            'description' => 'required',
            'rating' => 'nullable',
        ]);

        $review =  new Review;

        $review->rating = $request->input('rating');
        $review->reviewerid = auth()->user()->id;
        $review->productid = $id;
        $review->description = $request->input('description');

        $review->save();

        return redirect('/products/{$id}')->with('success', 'Review submitted');
    }

Upvotes: 1

Views: 485

Answers (1)

A.A Noman
A.A Noman

Reputation: 5270

You have to use like below code

{!! Form::open(['action' => ['App\Http\Controllers\PagesController@addreview', $product->id],'method' => 'POST']) !!}

and your route should be like below

Route::post('/addreview/{id}', 'App\Http\Controllers\PagesController@addreview');

Follow this instruction

Upvotes: 1

Related Questions