Eranda Dava
Eranda Dava

Reputation: 87

Update value from database on button click from Controller Laravel

I wanted to do update but do not need to go into edit

so i use this button to do the update

Button for update

when i click the green button, the value of "new" is going to change

this is my blade:

<form class="btn-group" action="{{ route('perjanjians.konfirmasi', ['id' => $item->id]) }}" method="POST">
                                                            @csrf
                                                            @method('PATCH')
                                                            <label class="btn btn-success text-center">
                                                                <button type="submit" class="btn btn-success btn-xs">Konfirmasi</button>
                                                            </label>
                                                        </form>

This is my Controller for update:

public function update_konfirmasi(Request $request, Perjanjian $perjanjian)
    {
        // dd($perjanjian);
        $request->validate([
            'schedule_doctor_id'    => 'nullable',
            'email'    => 'nullable',
            'no_tlp'    => 'nullable',
            'tanggal'    => 'nullable',
        ]);

        $perjanjian->nama_lengkap       = $request->get('nama_lengkap') ;
        $perjanjian->email              = $request->get('email') ;
        $perjanjian->no_tlp             = $request->get('no_tlp') ;
        $perjanjian->tanggal            = $request->get('tanggal') ;
        $perjanjian->is_confirm         = 'confirm';
        dd($perjanjian);
        $perjanjian->save();

        return back();
    }

but when i click, the data from $perjanjian variables its gone, but the is_confirm is changed

Like this

where i do something wrong when updating?

Upvotes: 0

Views: 1123

Answers (1)

Cl&#233;ment Baconnier
Cl&#233;ment Baconnier

Reputation: 6058

you're expliciting say they are null by doing

$perjanjian->nama_lengkap = $request->get('nama_lengkap')

Since you're not sending any value named nama_lengkap in your form request, it's equivalent to

$perjanjian->nama_lengkap = null

If you need to fill theses fields from an another form, I would suggest you to add a new and dedicated method for this one

public function confirm(Request $request, Perjanjian $perjanjian) {

        $perjanjian->is_confirm = 'confirm';
        dd($perjanjian);
        $perjanjian->save();

        return back();
}

Edit Also I noticed you're using ['id' => $item->id] I'm assuming your route is something like Route::patch('/confirm/{id}', ...)

Your method expect the parameter perjanjian.
Consequently it will be null since it receive id which will make a an empty model of Perjanjian.

You should change to ['perjanjian' => $item->id] and Route::patch('/confirm/{perjanjian}', ...) in order to make your Model Binding working as expected

Upvotes: 1

Related Questions