0

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?

2
  • if values can be nullable why to waste time on validating it, in your form you are just submitting your form, where are the values Commented Oct 6, 2020 at 14:09
  • the values is exists, i use nullable to test it if it was on validation issues. sorry i forgot to change it @bhucho Commented Oct 6, 2020 at 14:31

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

okay so the reason i use get is i try to get the old values of the fields. the values is exist, but when i try to update only 1 column, it turns other field to null
Can you add the full form ? It seems they are not sent then.
In case you didn't know $request->get('nama_lengkap') will get the value of <input name="nama_lengkap">. You don't need to re-assign $perjanjian->nama_lengkap = $previousValue. It won't be gone unless you say so.
@ErandaDava I have uptaded my answer
okay, i'll try to edit with your answer and come back here later, thankyou in advance
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.