[英]Problem with inserting and updating main video in database in Laravel project
I am working on a project for ads/properties in Laravel. 我正在为Laravel的广告/房产项目工作。 I have gallery of multiple videos for each individual ad. 我为每个广告都有多个视频库。 I want to be able to select one of those videos with radio button and make it primary video for that ad (show that particular video next to ad). 我希望能够使用单选按钮选择其中一个视频,并将其作为该广告的主要视频(在广告旁边显示该特定视频)。 I have has many relationship between property and video and I am trying to insert id of video from videos table to main_video_id column in properties table and also when I change current video for new one to update that foreign key accordingly. 我在属性和视频之间有很多关系,我正在尝试将视频的id从视频表插入属性表中的main_video_id列,并且当我更改新视频的当前视频以相应地更新该外键时。 I am having trouble to write that method in my controller. 我在控制器中编写该方法时遇到问题。 Any help is appreciated. 任何帮助表示赞赏。 Here are my tables and code. 这是我的表格和代码。
properties (id, title, location, price, main_video_id)
videos (id, filename_video, model_id)
In videos table model_id column is foreign key that connects to properties table. 在视频表中,model_id列是连接到属性表的外键。
PropertyController.php 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'
));
}
edit.blade.php edit.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">×</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
Property.php Property.php
public function videos()
{
return $this->hasMany(Video::class, 'model_id');
}
Video.php Video.php
public function properties()
{
return $this->belongsTo(Property::class);
}
Instead of having main_video_id
in the properties
table, I would go with the following approach: 我main_video_id
在properties
表中使用main_video_id
,而是使用以下方法:
Add a column is_main_video
to videos
table and a composite unique constraint as below: 将一列is_main_video
添加到videos
表和一个复合唯一约束,如下所示:
$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. 唯一约束确保单个属性没有多个主视频。
In Property.php
在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;
});
}
Now when you fetch properties
, you'll automatically get the mainVideo
and related videos as a result as show below: 现在,当您获取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"
]
]
]
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.