Laravel 8 PUT routes return 404

I’m trying to update data in the database via API but it returns error 404 not found in postman.

api.php

    // PUT update citizen from NIC
Route::put('updateCitizen/{nic}',[CitizensController::class, 'updateCitizen'] );

controller.php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelsCitizen as ModelsCitizen;
use DB;
use IlluminateSupportFacadesDB as FacadesDB;

class CitizensController extends Controller
{
    public function updateCitizen(Request $request, $nic)
    {

        $citizen = FacadesDB::table('citizens')
            ->where('nic', '=', $nic)
            ->update(['options->enabled', true]);

        return response()->json($citizen);
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

why you dont use a api resource approach insted, looks much more faster to write to imo.

https://laravel.com/docs/8.x/eloquent-resources

Otherwise use more ‘LIKE‘ as string operator for the where clause

Method 2

Can you change the API route as following?

Route::put('update-citizen/{nic}',[CitizensController::class, 'updateCitizen']);

and run the following command.

php artisan route:cache

and try with the following URL – {baseUrl}/api/update-citizen/{an-existing-nic}

Method 3

The problem was in the update query.
in the query where,

->update(['options->enabled', true]);

has to be changed as,

    ->update($request->all());

to update the all the vlaues in the uri to the database columns.

Method 4

top of your contoller put:

use IlluminateSupportFacadesDB;

then use:

$citizen = DB::table('citizens')
         ->where('nic',$nic)
         ->update(['options->enabled', true]);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x