123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Admin\Repositories;
- use App\Models\SitePreviewVideo as Model;
- use Dcat\Admin\Repositories\EloquentRepository;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- class SitePreviewVideo extends EloquentRepository
- {
- /**
- * Model.
- *
- * @var string
- */
- protected $eloquentClass = Model::class;
- /**
- *
- */
- public static function updatePreviewVideo($album_id, $data) {
- // Get existing videos from the database for the given album_id
- $model = new Model();
- $existingVideos = $model->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' => Carbon::now(),
- 'updated_at' => Carbon::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();
- }
- }
- }
- }
|