john_smith
john_smith

Reputation: 85

Storing Request to DB through controller logic results in error

I have the following Request function, it stores data to DB with this kind of validation:

This is the following code

public function request(Request $request) {
    $check = DB::table('lr2')->where('k_mk', $request->kode_mk)->first();
    $check2 = DB::table('lr2')->where('id_mhs', $request->user_id)->first();
    if(!$check && $check2) {
        DB::table('lr2')->insert([
            'id_mhs' => $request->user_id,
            'k_mk' => $request->kode_mk,
            'mk' => $request->mk,
            'request_seats' => $request->r_seats,
            'status_request' => '0',
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now()
        ]);
        return redirect()->back()->with('alert', 'Request telah dikirim');
    } else if($check && !$check2) {
        DB::table('lr2')->insert([
            'id_mhs' => $request->user_id,
            'k_mk' => $request->kode_mk,
            'mk' => $request->mk,
            'request_seats' => $request->r_seats,
            'status_request' => '0',
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now()
        ]);
        return redirect()->back()->with('alert', 'Request telah dikirim');
    } else if(!$check && !$check2) {
        DB::table('lr2')->insert([
            'id_mhs' => $request->user_id,
            'k_mk' => $request->kode_mk,
            'mk' => $request->mk,
            'request_seats' => $request->r_seats,
            'status_request' => '0',
            '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!');
    }
}

The problem is, I tried to send a form with id_mhs that don't exist but the k_mk is existed, by the logic above it should be sent just fine. (btw, in the DB, the id_mhs already exists, but it's coupled with different k_mk, so it should be fine, right?)

sqlyog

For example, I want to store a request containing k_mk = CE149 and the id_mhs is 4, but the program said the data already existed, when in fact it's not.

Upvotes: 0

Views: 31

Answers (1)

apokryfos
apokryfos

Reputation: 40730

From your description I think you need to check if the pair of values exists in the same row. The problem is your checks will fail even when those values exist in the database but are not in the same row. A way to fix that easily is:

$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' => '0',
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now()
    ]);
}

You can actually also use validation to do this:

public function request(Request $request){
     try {
         $request->validate([
             'kode_mk' => Rule::unique('lr2')->where(function ($q) use ($request) { $q->where('user_id', 'user_id'); })
         ]);
         DB::table('lr2')->insert([
            'id_mhs' => $request->user_id,
            'k_mk' => $request->kode_mk,
            'mk' => $request->mk,
            'request_seats' => $request->r_seats,
            'status_request' => '0',
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now()
         ]);
         return redirect()->back()->with('alert', 'Request telah dikirim');
     } catch (ValidationException $e) {
          return redirect()->back()->with('message', 'Data existed in DB!');
     }
}

You can also remove the try-catch block since Laravel will return with its own redirect and error message for a failing validation (more details on validation in the docs)

Upvotes: 1

Related Questions