Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Soo's avatar
Level 2

Laravel Update Method Returns Success but No Database Changes

Hello my friends i have a probleme: in my Laravel application where the update operation appears to succeed based on the response message, but no changes are actually reflected in the database. this is the code of the controller:

public function update(Request $request, $id)
{
 
    $request->validate([
        'changeName' => 'string|max:255',
        'file' => 'file|mimes:pdf,doc,docx|max:2048',
    ]);
    $changeRequest = ChangeRequest::find($id);
    
    $changeRequest->change_name = $request->input('changeName');

    // Gestion des fichiers
    if ($request->hasFile('file')) {
        foreach ($request->file('file') as $file) {
            $filePath = $file->store('change_requests');
            $changeRequest->file_name = $filePath; // Mettez à jour le champ file_name si vous le souhaitez
        }
    }
   
    $changeRequest->save();
   

    return response()->json(['message' => 'Change request updated successfully.']);
}

and the the code of react:

const handleSubmit = async (e) => {
        e.preventDefault(); // Empêche le rechargement de la page
        console.log('Change Name:', changeName); // Ajoutez cette ligne
    
        try {
            const formData = new FormData();
            formData.append('change_name', changeName); // currentChangeName est la valeur actuelle du champ
            files.forEach((file) => {
                formData.append('files[]', file);
            });
    
            // Envoyer la requête PUT à l'API
            await axiosClient.put(`/change-requests/${requestId}`, formData, {
                headers: {
                    'Content-Type': 'multipart/form-data',
                },
            });
    
            // Redirection après la mise à jour
            navigate("/projectsList");
        } catch (error) {
            console.error('Error updating change request:', error);
            setGlobalError('Error updating change request.'); // Afficher l'erreur
        }
    };
        

and this is the form

and this is the model code :

class ChangeRequest extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'project_name',
        'change_name',
        'file_name',
        'user_id',
        'start_at',
        'end_at',
        'link',
        'status',
    ];

any solution

0 likes
6 replies
manishchauhan's avatar

Write code with condition and check it

public function update(Request $request, $id) { $request->validate([ 'change_name' => 'string|max:255', 'files.*' => 'file|mimes:pdf,doc,docx|max:2048', ]);

$changeRequest = ChangeRequest::find($id);
if (!$changeRequest) {
    return response()->json(['message' => 'Change request not found.'], 404);
}

$changeRequest->change_name = $request->input('change_name');

if ($request->hasFile('files')) {
    $filePaths = [];
    foreach ($request->file('files') as $file) {
        $filePaths[] = $file->store('change_requests');
    }
    $changeRequest->file_name = json_encode($filePaths); // Store file paths as JSON
}

if ($changeRequest->save()) {
    return response()->json(['message' => 'Change request updated successfully.']);
} else {
    return response()->json(['message' => 'Failed to update change request.'], 500);
}

}

1 like
Snapey's avatar

Is the data different? If not, Eloquent will skip the update.

1 like
Soo's avatar
Level 2

@Snapey yes i change the name also i upload another file but the form data send emty data to the back

Snapey's avatar

How does changeName on the form become change_name in React and then changeName in the validation?

1 like
Soo's avatar
Soo
OP
Best Answer
Level 2

i found a solution i change the put request by a post and finaly it works .knowing that it does not display any errors.

Please or to participate in this conversation.