显示 table 中的一列在 Laravel 中有很多关系
Display one column from table in has many relationship in Laravel
在我的项目中,属性和视频之间有很多关系。我正在尝试显示属性 table 中的标题,其中该标题属于视频 table.
中的相应视频
properties (id, title)
videos (id, model_id, filename_video)
这里model_id是指向属性table的外键。使用当前代码,我可以显示所有标题。任何帮助表示赞赏。这是我的代码。
Property.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');
}
}
Video.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>
尝试这样设置:
Property.php
class Property extends Model
{
protected $guarded = ['id'];
public function video()
{
return $this->hasMany(Video::class);
}
}
Video.php
class Video extends Model
{
protected $guarded=['id'];
public function properties()
{
return $this->belongsTo(Property::class, 'model_id');
}
}
控制器
$results = Property::with('videos')->where('title', $property->title)->get();
你应该试试这个:
$results = DB::table('properties')
->join('videos', 'properties.id', '=', 'videos.model_id')
->pluck('title');
在我的项目中,属性和视频之间有很多关系。我正在尝试显示属性 table 中的标题,其中该标题属于视频 table.
中的相应视频properties (id, title)
videos (id, model_id, filename_video)
这里model_id是指向属性table的外键。使用当前代码,我可以显示所有标题。任何帮助表示赞赏。这是我的代码。
Property.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');
}
}
Video.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>
尝试这样设置:
Property.php
class Property extends Model
{
protected $guarded = ['id'];
public function video()
{
return $this->hasMany(Video::class);
}
}
Video.php
class Video extends Model
{
protected $guarded=['id'];
public function properties()
{
return $this->belongsTo(Property::class, 'model_id');
}
}
控制器
$results = Property::with('videos')->where('title', $property->title)->get();
你应该试试这个:
$results = DB::table('properties')
->join('videos', 'properties.id', '=', 'videos.model_id')
->pluck('title');