How to compare old created_at value with Carbon::now timestamp and update it to the new timestamp? - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, May 30, 2021

How to compare old created_at value with Carbon::now timestamp and update it to the new timestamp?

I have this request function to store data into DB

public function request(Request $request){
        $check = DB::table('lr2')->where('k_mk', $request->kode_mk)->where('id_mhs', $request->user_id)->exists();
        
        if(!$check){
        DB::table('lr2')->insert([
            'id_mhs' => $request->user_id,
            'k_mk' => $request->kode_mk,
            'mk' => $request->mk,
            'request_seats' => $request->r_seats,
            'status_request' => '1',
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now()
        ]);
        return redirect()->back()->with('alert', 'Request telah dikirim');
        }
        else{
            return redirect()->back()->with('message', 'Data existed in DB!');
        }
    }

It stores data correctly, and I also created an update function where if a new request came, just update the status request to 0 again and the old created_at and updated_at values to the new created_at and updated_at values.

public function request2(Request $request, $id){
        $check = DB::table('lr2')->where('k_mk', $request->kode_mk)->where('id_mhs', $request->user_id)->exists();
        if($check){
            DB::table('lr2')->where('id', $id)->update([
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
                'status_request' => '0'
             ]);
            return redirect()->back()->with('message', "Request telah dikirim kembali");
        }
        else if(!$check){
        return redirect()->back()->with('message', "Request gagal");
        }
    }

It works but only for status_request value, the old created_at and updated_values still persist. Why is it happen?



source https://stackoverflow.com/questions/67756801/how-to-compare-old-created-at-value-with-carbonnow-timestamp-and-update-it-to

No comments:

Post a Comment