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

rafidAhsan's avatar

laravel update() function not working

Class

protected $fillable = [
        'mobile_number',
        'method',
        'tracking_number',
        'amount',
        'user_id',
        'total',
        'approved'
    ];

Controller

    public function updateSavings(Request $request) {
        $request->validate([
            'mobile_number' => 'required',
            'method' => 'required',
            'tracking_number' => 'required',
            'amount' => 'required',
        ]);

        $savings = new SavingAcount();

        $savings->mobile_number = $request->mobile_number;
        $savings->method = $request->method;
        $savings->tracking_number = $request->tracking_number;
        $savings->amount = $request->amount;
        $savings->total = $request->total;
        $savings->user_id = $request->id;
        $savings->approved = 1;

        $savings->update();

        return redirect('/admin/savings')->with('status', 'savings Updated');
    }
0 likes
29 replies
Sergiu17's avatar
public function updateSavings(Request $request) {
	dd($request->all());
}

everything shows up here? amount, method and so on? you may have validation errors, and you are redirected back

tykus's avatar

Why are you newing up an instance and then calling update on it; it doesn't exist. You need to save, not update if the rest of the logic is correct, meaning you intend to create a new record in the database.

1 like
rafidAhsan's avatar

when i save this. it makes new record. But I want to update

tykus's avatar

But you do nothing to retrieve the record from that database that you want to update. How do you pass the id of the SavingAccount record to the updateSavings method?

tykus's avatar

In that case you should type hint a property on the method that matches the wildcard on the route definition, e.g.

public function updateSavings(Request $request, SavingAccount $savingAccount) {
    //...

No new needed

tykus's avatar

Can you show the full controller method as it is now?

rafidAhsan's avatar

Even cut the validation code

public function updateSavings(Request $request,  SavingAcount $savings) {
        $savings->mobile_number = $request->mobile_number;
        $savings->method = $request->method;
        $savings->tracking_number = $request->tracking_number;
        $savings->amount = $request->amount;
        $savings->total = $request->total;
        $savings->user_id = $request->id;
        $savings->approved = 1;

        $savings->update();

        return redirect('/admin/savings')->with('status', 'savings Updated');
    }
tykus's avatar

Use save not update; the update method takes an array of attributes

rafidAhsan's avatar

i know. But save method making new record instead of updating

tykus's avatar

In that case you have not understood earlier when I said that the typehinted argument must match the route wildcard. Show the route

rafidAhsan's avatar
Route::put('/admin/savings/update/{user_id}/{total}', 'AdminController@updateSavings');
tykus's avatar

You have a user_id and total as wildcards, that are unused in the controller action arguments?!! What are you doing???

Take a look at the docs for routing for goodness sake.

tykus's avatar

You must to pass the information necessary to identify the SavingAccount model!!! Anything else is form data

rafidAhsan's avatar
$savings->mobile_number = $request->mobile_number;
        $savings->method = $request->method;
        $savings->tracking_number = $request->tracking_number;
        $savings->amount = $request->amount;
        $savings->total = $request->total;
        $savings->user_id = $request->user_id;
        $savings->approved = 1;

        $savings->save();

on total and user_id. It comes from wildcard. this is not form data...

tykus's avatar
tykus
Best Answer
Level 104

Is Laravel supposed to guess the correct SavingAccount? You said you were getting the SavingAccount model from routing, you are not. If the User has just one SavingAccount associated then get it through the relationship

Please or to participate in this conversation.