SitePreviewVideo.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\SitePreviewVideo as Model;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. use Illuminate\Support\Carbon;
  6. use Illuminate\Support\Facades\DB;
  7. class SitePreviewVideo extends EloquentRepository
  8. {
  9. /**
  10. * Model.
  11. *
  12. * @var string
  13. */
  14. protected $eloquentClass = Model::class;
  15. /**
  16. *
  17. */
  18. public static function updatePreviewVideo($album_id, $data) {
  19. // Get existing videos from the database for the given album_id
  20. $model = new Model();
  21. $existingVideos = $model->where('album_id', $album_id)->get();
  22. // Iterate over the $data array
  23. foreach ($data as $value) {
  24. $videoSrc = $value['video_src'];
  25. // Check if the video_src already exists in the database
  26. $existingVideo = $existingVideos->firstWhere('video_url', $videoSrc);
  27. if ($existingVideo) {
  28. // If the video exists and the video_url is the same, do nothing
  29. continue;
  30. } else {
  31. // If the video_src doesn't exist, insert a new record
  32. $model->create([
  33. 'video_url' => $videoSrc,
  34. 'preview_url' => '',
  35. 'status' => 0, // Assuming status 1 means active or available
  36. 'created_at' => Carbon::now(),
  37. 'updated_at' => Carbon::now(),
  38. 'album_id' => $album_id,
  39. 'remark' => '', // Optional remark, you can customize this
  40. ]);
  41. }
  42. }
  43. // Now check for videos that are in the database but not in the $data array, and delete them
  44. foreach ($existingVideos as $existing) {
  45. $videoExistsInData = false;
  46. // Check if the current video exists in the data array
  47. foreach ($data as $value) {
  48. if ($value['video_src'] === $existing->video_url) {
  49. $videoExistsInData = true;
  50. break;
  51. }
  52. }
  53. // If the video does not exist in the data, delete it
  54. if (!$videoExistsInData) {
  55. $existing->delete();
  56. }
  57. }
  58. }
  59. }