ronyamin's avatar

How to get the post id in laravel

Am trying to update a record in my database using an update button that is linked to a page where the update form is located but each time I click on the button, I get a 404 error (I think maybe the problem is that the page could not read the id of the requested post).

This is my route

Route::get('/pages/update/{id}','CallsController@edit');
Route::post('/pages/update/{id}','CallsController@update');

My CallsController

public function edit($id)
    {
        // $calls = Call::where('user_id', auth()->user()->id)->where('id', $id)->first();
        // return view('pages.assignCall', compact('calls', 'id'));

        $calls = Call::find($id);
        return view('pages.assignCall')->with('calls', $calls);
    }

public function update(Request $request, $id)
    {
        $calls = new Call();
        $data = $this->validate($request, [
            'call_details'=>'required',
        ]);
        $data['id'] = $id;
        $calls->updateCall($data);

        return redirect('pages.pendingCalls');
    }

I also have update.blade.php in the pages folder inside my views.

This is my update button

<a href="{{asset('/pages/update')}}>update</a>
0 likes
1 reply
tisuchi's avatar
tisuchi
Best Answer
Level 70

@ruhulamin

You should not use the asset helpers to generate urls, but the url helpers. Also, you have to pass the id of the item you want to update. Because this is missing, you get a 404.

You should do something like this:

<a href="{{ url('/pages/update/{$page->id}') }}">update</a>
10 likes

Please or to participate in this conversation.