【发布时间】:2014-01-21 02:23:10
【问题描述】:
我认为这将是 hashasMany 和 belongsTo 的简单用法,但我没有获取我的一个表的数据,即标题。
我有两张桌子:
scenes ( movie scenes basically)
------
int scene_id (can't change to "id".. this is at my company)
string scene_name
int title_id
titles (movie dvd titles)
------
int title_id ( same here.. should be id, but it's not )
string title_name
string title_description
“场景”表中有多个场景,具有相同的 title_id,您可能已经猜到了。
到目前为止,我有:
class Title extends Eloquent {
protected $table = 'titles';
protected $primaryKey = "title_id";
public function scenes() {
return $this->hasMany('Scene','title_id','title_id');
}
}
class Scene extends Eloquent {
protected $table = 'scenes';
protected $primaryKey = "scene_id";
public function title() {
return $this->belongsTo('Title', 'title_id', 'title_id');
}
}
在我看来,我必须为 title() 和 Scenes() 成员函数指定 title_id。但该字段将每个标题与其多个场景连接起来。
我为获得一个场景(假设 $scene_id 是一个 int)以及每个场景的标题信息而进行的调用是:
$scene_info = Scene::find($scene_id)->title();
如果我对 $scene_info 执行 var_dump,我会得到 很多 的 Laravel 代码,我猜我不应该这样做。
我也得到了场景信息,但是标题信息是空白的。
只是想知道我的 Laravel 编码是否有问题
史蒂夫
【问题讨论】:
-
请向我们展示此
lot of Laravel code的前三行,这将有助于人们了解您所得到的。 -
抱歉,在我发布这个问题时我没有看到您的评论。我会发布该信息的。