where('album_id', $album_id)->get(); // Iterate over the $data array foreach ($data as $value) { $videoSrc = $value['video_src']; // Check if the video_src already exists in the database $existingVideo = $existingVideos->firstWhere('video_url', $videoSrc); if ($existingVideo) { // If the video exists and the video_url is the same, do nothing continue; } else { // If the video_src doesn't exist, insert a new record $model->create([ 'video_url' => $videoSrc, 'preview_url' => '', 'status' => 0, // Assuming status 1 means active or available 'created_at' => Corder::now(), 'updated_at' => Corder::now(), 'album_id' => $album_id, 'remark' => '', // Optional remark, you can customize this ]); } } // Now check for videos that are in the database but not in the $data array, and delete them foreach ($existingVideos as $existing) { $videoExistsInData = false; // Check if the current video exists in the data array foreach ($data as $value) { if ($value['video_src'] === $existing->video_url) { $videoExistsInData = true; break; } } // If the video does not exist in the data, delete it if (!$videoExistsInData) { $existing->delete(); } } } }