I have 3 tables. 1 that defines videos, 1 that defines carousels and 1 pivot table that defines which carousel a video belongs to if it even belongs to any.
我有3张桌子。 1定义视频,1定义轮播和1个数据透视表,定义视频属于哪个轮播,如果它甚至属于任何。
I have tried to describe my tables below in as simple a way as possible
我试图以尽可能简单的方式描述下面的表格
- Videos Table
id title user
id标题用户
- Feature Table (Pivot)
功能表(Pivot)
id carousel_id video_id
id carousel_id video_id
- Carousel Table
id carousel_type
How can I query the database for all videos in the Feature table with a given carousel type using Eloquent models relations. I am using Laravel 5 if that makes a difference. I have tried the morphMany as described in their documentation but I must be doing something wrong.
如何使用Eloquent模型关系使用给定的轮播类型在Feature表中查询所有视频的所有视频。如果这有所作为,我正在使用Laravel 5。我已按照文档中的描述尝试了morphMany,但我一定做错了。
Thanks!
Edit:
Table names:
videos, features, carousels
视频,功能,旋转木马
Model names:
Video, Feature, Carousel
视频,功能,旋转木马
#Edit 2: Tables: casts,features,featureables Models:Cast, Feature
编辑2:表格:强制转换,特征,功能特征模型:强制转换,特征
Here are the files that I am currently having trouble with.
以下是我目前遇到问题的文件。
Controller:
class castController extends Controller {
public function index()
{
$carousel_casts = \App\Feature::find(1)->casts;
foreach($carousel_casts as $casts){
echo $casts->title . "<br>";
}
}
}
Model:
public function casts()
{
return $this->belongsToMany('\App\Cast','featureables');
}
I have specific table names for a reason, I am taking over a pre existing project and the table names can't change. The current tables are casts(video), features(the carousel table), featureables(the pivot table).
我有特定的表名是有原因的,我接管一个预先存在的项目,表名不能改变。当前表是强制转换(视频),功能(轮播表),功能表(数据透视表)。
I can query all of these tables seperatly without issue, however when I use the belongsToMany relationship I get the following error.
我可以毫无问题地单独查询所有这些表,但是当我使用belongsToMany关系时,我得到以下错误。
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'featureables.feature_id' in 'field list' (SQL: select casts
.*, featureables
.feature_id
as pivot_feature_id
, featureables
.cast_id
as pivot_cast_id
from casts
inner join featureables
on casts
.id
= featureables
.cast_id
where featureables
.feature_id
= 1)
SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'featureables.feature_id'(SQL:select casts。*,featureables.feature_id as pivot_feature_id,featureables.cast_id as pivot_cast_id from casts inner join featureables on casts.id = featureables.cast_id,其中featureables.feature_id = 1)
1 个解决方案
#1
First, rename features
table to carousel_video
.
首先,将功能表重命名为carousel_video。
Next, define relationship in your Carousel
model like:
接下来,在您的Carousel模型中定义关系,如:
public function videos()
{
return $this->belongsToMany('YourAppNamespace\Video');
}
Then, query the Carousel
model like:
然后,查询Carousel模型,如:
$videos = Carousel::find(2)->videos; //finds all videos associated with carousel having id of 2
return $videos;
You can do the opposite by defining a relationship on your Video
model like:
您可以通过在视频模型上定义关系来执行相反的操作,例如:
public function carousels()
{
return $this->belongsToMany('YourAppNamespace\Carousel');
}
And, querying like:
并且,查询如下:
$carousels = Video::find(2)->carousels; //finds all carousels associated with video having id of 2
return $carousels;
#1
First, rename features
table to carousel_video
.
首先,将功能表重命名为carousel_video。
Next, define relationship in your Carousel
model like:
接下来,在您的Carousel模型中定义关系,如:
public function videos()
{
return $this->belongsToMany('YourAppNamespace\Video');
}
Then, query the Carousel
model like:
然后,查询Carousel模型,如:
$videos = Carousel::find(2)->videos; //finds all videos associated with carousel having id of 2
return $videos;
You can do the opposite by defining a relationship on your Video
model like:
您可以通过在视频模型上定义关系来执行相反的操作,例如:
public function carousels()
{
return $this->belongsToMany('YourAppNamespace\Carousel');
}
And, querying like:
并且,查询如下:
$carousels = Video::find(2)->carousels; //finds all carousels associated with video having id of 2
return $carousels;