php - 在 Laravel 项目的数据库中插入和更新主视频的问题

标签 php laravel

我正在为 Laravel 中的广告/属性项目工作。我有每个广告的多个视频库。我希望能够使用单选按钮选择其中一个视频并将其作为该广告的主要视频(在广告旁边显示该特定视频)。我在属性和视频之间有很多关系,我试图将视频表中的视频 ID 插入属性表中的 main_video_id 列,并且当我将当前视频更改为新视频以相应地更新该外键时。我无法在我的 Controller 中编写该方法。任何帮助表示赞赏。这是我的表格和代码。

properties (id, title, location, price, main_video_id)

videos (id, filename_video, model_id)

在视频表中,model_id 列是连接到属性表的外键。

PropertyController.php

public function edit(Property $property, Request $request)
{
    if (!($property->user_id == Auth::user()->id)) {
        abort(404);
    }

    $category = $property->category;

    foreach ($property->photos as $value) {
        $photoarray[] = $value->filename;
    };

    empty($photoarray) ? $photo = '' : $photo = implode(",", $photoarray);

    foreach ($property->videos as $value1) {
        $videoArray[] = $value1->filename_video;
    };

    empty($videoArray) ? $video = '' : $video = implode(",", $videoArray);


    $data = .... Here I am having trouble writing method!


    return view('property.edit', compact(
        'category',
        'photo',
        'property',
        'video', 
        'data'
    ));
}

编辑.blade.php

<form id="form" action="/property/{{$property->id}}" method="POST" enctype="multipart/form-data">
<input type="hidden" id="hideninput" data-src="property/{{$property->id}}/gallery"value="{{old('video',$video)}}" name="video">
    @if (old('video', $video))
    @foreach (array_filter(explode(',',old('video', $video)), 'strlen') as $key =>$value)
        <div id="{{'div_video_'.$key}}" class="col-md-3" style="padding: 15px;">

            @foreach ($data as $key => $value)
                <input type="radio" name="radio" value="{{ $value->video_id }}">Make main
            @endforeach

            <button data="{{$key}}" type="button" class="closebuttonvideo">
                <span aria-hidden="true">&times;</span>
            </button>
            <video id="{{'video_'.$key}}" data={{$value}} src="/storage/property/{{$property->id}}/gallery/{{$value}}" class="video-js vjs-default-skin" controls preload="auto" data-setup='{"inactivityTimeout": 0}' width="180" height="180">
            </video>
        </div>
    @endforeach
    @endif

属性.php

public function videos()
{
    return $this->hasMany(Video::class, 'model_id');
}

视频.php

public function properties()
{
    return $this->belongsTo(Property::class);
}

最佳答案

我不会在 properties 表中使用 main_video_id,而是采用以下方法:

videos 表添加一列 is_main_video 和如下复合唯一约束:

$table->boolean('is_main_video')->nullable()->default(null);
$table->unique(['model_id', 'is_main_video']);

The unique constraint ensures that there are no multiple main videos for a single property.

Property.php

protected $appends = ['mainVideo'];

public function videos()
{
   return $this->hasMany(Video::class, 'model_id');
}

public function getMainVideoAttribute()
{
    return $this->videos->first(function($video) {
         return $video->is_main_vide;
    });
}

现在,当您获取properties 时,您将自动获取mainVideo 和相关视频,如下所示:

array:4 [
  "id" => 1
  "name" => "property 1"
  "mainVideo" => array:4 [
    "id" => 2
    "model_id" => 1
    "is_main_video" => 1
    "name" => "Video 2"
  ]
  "videos" => array:2 [
    0 => array:4 [
      "id" => 1
      "model_id" => 1
      "is_main_video" => null
      "name" => "Video 1"
    ]
    1 => array:4 [
      "id" => 2
      "model_id" => 1
      "is_main_video" => 1
      "name" => "Video 2"
    ]
  ]
]

希望对您有所帮助!

关于php - 在 Laravel 项目的数据库中插入和更新主视频的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57041355/

相关文章:

php - 构建高度可扩展的 Web 服务

laravel - 反射异常 : Class db does not exist

PHP 和 Python 接口(interface)

PHP:PDO 的自定义错误处理程序?

php - 无法看到响应获取请求 laravel phpunit

php - PHP 中的代码优先 Entity Framework 模型

php - 为什么在进行集成测试时必须重新加载 Auth::user()

laravel - 将参数传递给 Laravel 作业不起作用

php - strtotime 在使用硬编码字符串形式的值时不返回值

php - 使用 PHP 将 CSV 文件导入 MySQL