Dynow
Dynow

Reputation: 11

Delete and Update Method of CRUD is not passing all the parameters

I've implemented a modal type Update and Delete functions in my website but it always return Too few arguments to function App\Http\Controllers\AdminController::destroy(), 1 passed in D:\SUDRTest\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected it is also the same for the Update function as well

Here is my route for the CRUD

 Route::resource('papers', AdminController::class)->only(['edit', 'update', 'destroy']);

Here is the View

<li class="pdfpaperInfo">
                <div class="colpdf col-1" data-label="Title:">{{ $paper->PaperTitle }}</div>
                <div class="colpdf" data-label="Paper Type:">{{ $paper->PaperType }}</div>
                <div class="colpdf" data-label="College:">{{ $paper->College }}</div>
                <div class="colpdf" data-label="Author(s):">{{ $paper->Authors }}</div>
                <div class="colpdf" data-label="Date Published:">{{ $paper->DatePublished }}</div>
                <div class="pdfbtnCont">
                    <button class="pdfBtn redBtn" onclick="location.href='{{route('MyProfile')}}'">Back</button>
                    <button class="pdfBtn redBtn" id="modalOneBtn" onclick="location.href='{{route('papers.edit', $paper->PaperID)}}'">Update</button>
                    <button class="pdfBtn redBtn" id="modalTwoBtn">Delete</button>
                </div>
            </li>

            <div id="modalOne" class="modal">

                <!-- Modal content -->
                <div class="modal-content">
                    <span class="m1Close close">&times;</span>
                    <div class="modalinfoCont">

                        <h2>Update Paper</h2>

                            @include('admin.updatepaper')
                    </div>
                </div>
            
            </div>

            <div id="modalTwo" class="modal">

                <!-- Modal content -->
                <div class="modal-content">
                    <span class="m2Close close">&times;</span>
                    <div class="modalTwoCont modalinfoCont">

                        <h2>Delete Paper</h2>
                        <br>
                        Are you sure you want to delete this paper?
                        <br>
                        <br>
                        <div class="modalbtnCont">
                            <form method="POST" action="{{route('papers.destroy', $paper->PaperID) }}">
                            @csrf
                            @method('DELETE')
                                <button class="redBtn" type="submit">Yes</button>
                            </form>
                            <button class="redBtn" type="submit">No</button>
                        </div>
                    </div>
                </div>
            
            </div>

        </div>

and the controller

public function destroy(Papers $paper, $PaperID)
   {
       $paper=Papers::find($PaperID);
       $paper->delete();
       return redirect()->back();
   }

   public function edit(Papers $paper, $PaperID)
   {
       $paper=Papers::find($PaperID);
       return view('admin.updatepaper',compact('paper'));
   }

   public function update(Request $request,Papers $paper, $PaperID )
   {
        $request->validate([
            'PaperTitle' => 'required',
            'PaperType' => 'required',
            'file' => [
                'required',
                File::types('pdf')
                    ->max(12 * 1024),
        ],
    ]);

        $paper=new Papers();

        $file=$request->file;

        $filename=time().'.'.$file->getClientOriginalExtension();
                $request->file->move('assets', $filename);
                $paper->file=$filename;

            $paper->DatePublished=$request->DatePublished;
            $paper->PaperTitle=$request->PaperTitle;
            $paper->PaperType=$request->PaperType;
            $paper->Authors=$request->Authors;


            $paper->update();
            return redirect()->back();
   }

I've tried not to do it in modal form and still it kept on displaying the same error and I don't know what is the missing parameter since it doesn't tell me

Upvotes: 0

Views: 99

Answers (1)

Ali Rahimi
Ali Rahimi

Reputation: 402

You need to take another look at route-model binding.

Laravel will by default do the Papers::find($paperID) and pass the Papers model as the Papers $papers argument to your methods.

So the destroy method should be:

   public function destroy(Papers $paper)
   {
       $paper->delete();
       return redirect()->back();
   }

Of course you can disable route-model binding and do your own thing but it doesn't seem necessary here.

Its not clear what you intend to do in the update method. If you want to create a new paper on update and keep the old one then change $paper->update() to $paper->save() and you should be good. But if you want to do an actual update you should do something like this:

update(Papers $paper, Request $request) {
    // validate

    $paper->DatePublished=$request->DatePublished;
    // update other fields

    $paper->save();

     return redirect()->back();
}

Upvotes: 0

Related Questions