Prince Kumar Barnwal
Prince Kumar Barnwal

Reputation: 472

Put method not working to pass parameter in laraverl

My route

Route::put('/users/{$id}', 'ApiController@profileUpdate')->name('user.update');

//profile update function public function profileUpdate(Request $request){

    try{

        $validator = $this->validatorProfile($request->all());

        if ($validator->fails()) {
            $messages = $validator->messages();
            return response()->json([
                'status' => 400,
                'error'  => true,
                'result' => false ,
                'message'=> $messages,
                'data'   => []
            ]);
        }

        $id = $request->id;
        $token = $request->header('authorization');
        $user_id = JWTAuth::toUser($token)->id;
        $user = User::find($user_id);
        $data = $request->only('location','state','country','name');



        $result = $user->profiles()->where(['user_id' => $$id])->update([
            'location'     => $data['location'],
            'state'        => $data['state'],
            'country'      => $data['country'],
        ]);

        $result1 = $user->where(['id' => $user_id])->update([

            'name'     => $data['name'],
        ]);

        return response()->json([
            'status' => 200,
            'error'  => false,
            'result' => true, 
            'message'=> "profile updated",
            'data'   => $data
        ]); 
    }
    catch(Exception $e){
        return response()->json([
            'status' => 400,
            'error'  => true,
            'result' => false, 
            'message'=> $e,
            'data'   => []
        ]);
        dd($e);
    }
}

Help me to find my mistake.

My url

http://localhost/project/public/api/v1/users/1

When i hit it on postman it give 404 error.

Upvotes: 1

Views: 110

Answers (2)

lagbox
lagbox

Reputation: 50491

You will have to adjust your route parameter choice of naming. {$id} isn't going to work, but {id} will work just fine. Also the $$id reference in the code probably should be just $id.

As Alexey Mezenin has pointed out, it additionally would be good to add the $id parameter to your controller method to accept the route parameter.

Side Note:

When you are calling $request->id you are probably trying to get the route parameter, but that may not always return that. If there is any input in the Request named id that will be returned before a route parameter named id. It only tries to return a route parameter as a fallback.

$request->input('id') explicitly ask for an input named id.

$request->route('id') explicitly ask for a route parameter named id.

$request->id could be an input named id or a route parameter named id.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Since you're trying to pass ID, you need to add it as an argument. So, change this:

public function profileUpdate(Request $request)

To:

public function profileUpdate(Request $request, $id)

Upvotes: 0

Related Questions