Update of data from Laravel user table

Asked

Viewed 127 times

0

I’m trying to perform an update, the successful json Sponse is returned, but when I consult the database is not changing the data.

I tried to:

Route::put('alteraDadosPerfil/{id}', 'Auth\LoginController@alteraDadosPerfil');

function alteraDadosPerfil(Request $request, $id){

     User::findOrFail($id)->first()->fill($request->all())->save();
     return response()->json('Dados de perfil alterados com sucesso', 200);
}

You have to perform some additional procedure to update the users table generated by the Standard?

1 answer

2


When you have the findOrFail() doesn’t need the first(). And instead of using the fill(), use the method update().

function alteraDadosPerfil(Request $request, $id){
    try{
        \DB::transaction(function() use($request){
            User::findOrFail($id)
                ->update($request->all());
        });
    } catch(\Exception $e){
        return response()->json([$e], 500) 
    }

    return response()->json(['Dados de perfil alterados com sucesso'], 200)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.