Laravel 8 PUT routes return 404

Viewed 191

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 App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Citizen as ModelsCitizen;
use DB;
use Illuminate\Support\Facades\DB 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);
    }
}
4 Answers

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

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}

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.

top of your contoller put:

use Illuminate\Support\Facades\DB;

then use:

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