mercredi 24 avril 2019

How to fix "Too few arguments to function App\Http\Controllers\..\Controller::edit(), 0 passed and exactly 1 expected" error in Laravel

I want to show Edit page and update a record whenever user clicks on EDIT button on the records table on index page.

It's basically about the EDIT function in a typical CRUD operation in Laravel 5.8

I have tried several questions and answers suggested by stackoverflow before finally deciding to ask this question because no of the answers worked for me.

Edit function in controller

public function edit($id)
{
    $grantapplications = grantapplications::findOrfail($id);

    return view('member.grant_apply.edit', compact('grantapplications'));
}

Member Dashboard Routes

Route::group(['namespace' => 'Member'], function()
{
    Route::get('/grant_apply/edit', 'GrantApplicationsController@edit')
        ->name('member.grant_apply.edit');
    Route::post('/grant_apply/edit/{id}', 'GrantApplicationsController@update')
        ->name('member.grant_apply.update');
});

Edit.blade

<form method="post" action="">
    @method('PATCH')
        @csrf

Submit Button on Edit.blade

<input class="btn btn-lg btn-primary btn-block"
       type="submit" 
       value="Update!">

Just in case you might want to see my update() function

public function update(Request $request, $id)
{
    $request->validate([
        'project_category' => 'required',
        'project_title' => 'required',
        'project_desc' => 'required',
        'project_location' => 'required',
        'grant_goal' => 'required',
        'start_date' => 'required',
        'end_date' => 'required'
    ]);

    $grantapplications = grantapplications::find($id);
    $project_category->project_category = $request->get('project_category');
    $project_title->project_title =  $request->get('project_title');
    $project_desc->project_desc = $request->get('project_desc');
    $project_location->project_location  = $request->get('project_location');
    $grant_goal->grant_goal = $request->get('grant_goal');
    $start_date->start_date = $request->get('start_date');
    $end_date->end_date = $request->get('end_date');

    $grantapplications->save();

    return redirect('/member/grant_apply/index')
        ->with('success', 'Application is successfully updated!');
}

Meanwhile, the grantapplications id actually actually exists in the DB.

I expect to be able to edit and update a record successfully without throwing such error.

Looking forward with thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire