0

i am trying to check array of data, so user cannot add same data at once. what my code look like:

        for ($count = 0; $count < count($class_id); $count++) {
            $data = array(
                'class_id' => $class_id[$count],
                'section_id' => $section_id[$count],
                'teacher_id' => $teacher_id[$count],
                'days' => $days[$count],
                'Start_Time' => $start_time[$count],
            );
            $insert_data[] = $data;

        }



        for($count=1; $count<count($insert_data);$count++){
            if($insert_data[$count]['class_id']&&$insert_data[$count]['section_id']&&$insert_data[$count]['teacher_id']
            &&$insert_data[$count]['days']&&$insert_data[$count]['Start_Time']){
                return response()->json([
                    'error' => 'Same data cannot be insert'
                ]);
            }
        }


         Timetable::insert($insert_data);
            return response()->json([
                'success' => 'Data Added successfully.',
                'redirect' => url('/manage/timetable'),
            ]);

        }

    return redirect('/manage/timetable')->with('message','Data Added Successfully!');

}

data inserted correctly, everything working fine, what i want to do is to check if user insert same data 2 time or not? i uses for loop for that. what i am doing wrong? it give me "Cannot add same data" even if i change i field still gives me this error. new to laravel.

Fahim Khan
  • 153
  • 2
  • 3
  • 12
  • Where you checked duplicated data? in your second loop , if condition only check that value is exist or not. thats why you get error every time – Yasin Patel Nov 13 '19 at 07:23
  • you are not checking if any values are equal to any other values, you are just checking if there are true values in each index of the array – lagbox Nov 13 '19 at 07:24
  • than how to check whether data is duplicated or not? @YasinPatel – Fahim Khan Nov 13 '19 at 07:24
  • @lagbox Thank for answering! so how to check whether data is duplicated or not? Look at the interface : https://prnt.sc/pw8swg Admin can create as many as he want! so how to check whether admin added a duplicated data or not? – Fahim Khan Nov 13 '19 at 07:35

1 Answers1

0

You can filter duplicated value in array using this solution from here

for ($count = 0; $count < count($class_id); $count++) {
    $data = array(
                'class_id' => $class_id[$count],
                'section_id' => $section_id[$count],
                'teacher_id' => $teacher_id[$count],
                'days' => $days[$count],
                'Start_Time' => $start_time[$count],
  );
  $insert_data[] = $data;

}

$toInsert= array_map("unserialize", array_unique(array_map("serialize", $insert_data)));

Timetable::insert($toInsert);
   return response()->json([
      'success' => 'Data Added successfully.',
      'redirect' => url('/manage/timetable'),
]);
Poldo
  • 1,924
  • 1
  • 11
  • 27
  • firstOrCreate? where and how? can you explain a lil bit? – Fahim Khan Nov 13 '19 at 07:38
  • what i want to do is to check array of data. i mean if admin duplicated the data so message is display that you cannot insert duplicated data. and what i done is i create a add more button that create as many instance as admin want, so i want to check all data at once whether admin added a duplicated data or not – Fahim Khan Nov 13 '19 at 07:47
  • @FahimKhan you mean you want to filter same data in array? – Poldo Nov 13 '19 at 07:55
  • yeah, i want to check whether admin is adding same data twice or trice or not? if data is duplicated so shows a message duplicated data cannot be insert! – Fahim Khan Nov 13 '19 at 07:57
  • This method is useful when you don't want to insert duplicated entry in database! but how to tell user whether they submit same values. – Fahim Khan Nov 13 '19 at 10:54