Mr Android
Mr Android

Reputation: 47

Update data in popup modal in laravel

I need to update user data from a pop modal in laravel, but I'm unable to. Everything stays the same in the database.

I'm still new to laravel and I have a tight deadline. This is driving me crazy. I know I'm probably just doing something stupid, but I'm struggling to see what it is.

When I hit the submit button to update the data, nothing happens.

web.php


/* TEACHERS MATTERS*/
Route::get('timetable/{user_id}', [TimetableController::class, 'create'])->name('timetable.up');
Route::post('timetable/{user_id}', [TimetableController::class, 'store']);
Route::get('timetable', [TimetableController::class, 'create']);
Route::post('timetable', [TimetableController::class, 'store']);
Route::Post('timetable/update', [TimetableController::class, 'update'])->name('timetable/update');

In the above, this is the route:


Route::Post('timetable/update', [TimetableController::class, 'update'])->name('timetable/update');

TimetableController:

 public function update(Request $request)
    {
        $timetable = Timetable::find($request->get('id'));
        $timetable['tea_id'] = $request->get('tea_id');
        $timetable['subject'] = $request->get('subject');
        $timetable['class'] = $request->get('class');
        $timetable['day'] = $request->get('day');
        $timetable['start_time'] = $request->get('start_time');
        $timetable['close_time'] = $request->get('close_time');
        $update = $timetable->save();
        if($update){
            return redirect()->back()->with('success', 'New Schedule Created');
        }else{
            return redirect()->back()->with('failed', 'Update failed');
        }
    }
    public function status($id, $status){
//        echo $status;
//        dd($id);
        $timetable = Timetable::find($id);
        $timetable['status'] = $status;
        $update = $timetable->save();
        if($update){
            return redirect()->back()->with('success', 'Status changed');
        }else{
            return redirect()->back()->with('failed', 'Action not performed');
        }
    }

Here's the db image:

enter image description here

timeblade.php


  <!-- Start of modal update  -->
                        <div class="modal fade" id="update" tabindex="-1" role="dialog" aria-
                                    labelledby="demoModalLabel" aria-hidden="true">
                                    <div class="modal-dialog" role="document">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="demoModalLabel">Change day </h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-
                                                    label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>

                                            <form action="{{ route('timetable/update') }}" method="post">

                                                <center>
                                                    @csrf

                                                    <div style="width: 90%; ">
                                                        <input type="text" class="form-control" name="day"
                                                            id="day" aria-describedby="basic-addon1"
                                                            style="border-radius: 5px;">

                                                    </div>
                                                </center>

                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-outline-danger" data-dismiss="modal"
                                                        aria-label="Close">Close</button>
                                                        
                                                    <button type="submit" class="btn btn-outline-success">Save
                                                        changes</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                                <!-- End of modal update -->


Upvotes: 2

Views: 1080

Answers (1)

ne0teric
ne0teric

Reputation: 251

You used array syntax instead of object syntax for your Eloquent Model. see https://laravel.com/docs/8.x/eloquent#updates

Correction :

public function update(Request $request)
{
    $timetable = Timetable::find($request->get('id'));
    $timetable->tea_id = $request->get('tea_id');
    $timetable->subject = $request->get('subject');
    $timetable->class = $request->get('class');
    $timetable->day = $request->get('day');
    $timetable->start_time = $request->get('start_time');
    $timetable->close_time = $request->get('close_time');
    $update = $timetable->save();
    if($update){
        return redirect()->back()->with('success', 'New Schedule Created');
    } else {
        return redirect()->back()->with('failed', 'Update failed');
    }
}

This snippet is missing validation and some checks (for example, you should check if your model exists in database)

Upvotes: 1

Related Questions