【发布时间】:2019-07-12 09:31:28
【问题描述】:
在我的项目中,属性和视频之间有很多关系。我正在尝试从属性表中显示标题,其中该标题属于视频表中的相应视频。
properties (id, title)
videos (id, model_id, filename_video)
这里的model_id是指向属性表的外键。使用当前代码,我可以显示所有标题。任何帮助表示赞赏。这是我的代码。
属性.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Property extends Model
{
protected $guarded = ['id'];
public function videos()
{
return $this->hasMany(Video::class, 'model_id');
}
}
视频.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
protected $guarded=['id'];
public function properties()
{
return $this->belongsTo(Property::class);
}
}
PropertyController.php
public function viewVideos(Property $property, Video $video)
{
$results = DB::table('properties')
->join('videos', 'properties.id', '=', 'videos.model_id')
->select('properties.title')
->get();
$video = $property->videos;
return view('property.videos', compact('video', 'results'));
}
videos.blade.php
<h1 class="font-weight-light text-center text-lg-left mt-4 mb-0">
Videos for
@foreach($results as $result)
{{$result->title}}
@endforeach
</h1>
【问题讨论】:
-
您到底在显示什么?带有属性标题或属性列表的视频列表?
-
@Karan 我有那个页面视频,其中列出了该属性的所有视频。我正在尝试在页面顶部显示该属性的标题。例如,“该属性的标题”的视频。 '
-
你得到什么结果查询的输出?
-
@Karan Collection {#297 ▼ #items: array:2 [▼ 0 => {#307 ▼ +"title": "aaaaaaaaaaaaaaaaaaaa" } 1 => {#304 ▼ +"title" :“这是项目一的标题!!!” } ] }
-
@Karan 或者简单地说,来自不同属性的标题。我需要一个特定的标题。在这种情况下,“项目一的这个标题!!! '