Octobercms / Laravel根据关系获得id

时间:2016-06-10 10:18:19

标签: php laravel relationship octobercms

在Octobercms中,我有一个节目列表(主要)。他们与四季和季节有很多关系。然后我有属于季节的剧集(季节有很多剧集)

在下面的图片中,我想要的是获得属于该节目的季节,而不是每个创建的季节都是如下所示。有laravel或octobercms知识的人是否知道如何实现这个?

octobercms的工作方式是通过关系填充下拉列表或通过模型手动填充。我是通过关系来做的:

$date30DaysBack= Carbon::now()->subDays(30)->format('d-m-Y'); 
$today = Carbon::now()->format('d-m-Y'); 

 $adverts = DB::table('adverts') 
   ->whereBetween(DB::raw('STR_TO_DATE(instances.date,"%d-%m-%Y")'),[$date30DaysBack,$today])->get()

这两个链接会有所帮助,但不是我想要得到的: http://octobercms.com/docs/database/relations
http://octobercms.com/docs/backend/relations

http://image.prntscr.com/image/a0fa04481cc04ac5ad9af6371bace94d.png

2 个答案:

答案 0 :(得分:0)

Create Episodes模式的背景是什么?当然,如果节目不在模态中,那么该节目应该被定义吗?

据我了解,你的关系是这样的:

Show hasMany Season

Season hasMany Episode

我建议尝试使用特定的Show Season填充下拉菜单,然后您只会看到属于Season的{​​{1}} Show

所以在代码看起来像这样......

让我们说我们希望获得节目的所有季节,ID为1:

$show = Show::find(1);

$seasons = $show->seasons; // seasons that belong to show where id = 1

希望有帮助吗?

答案 1 :(得分:0)

我使用Request :: segement()来获取节目的ID,在下拉函数下的Episode模型中:

public function getSeasonOptions()
{
    return Season::where('anime_id', Request::segment(6))->lists('title', 'id');
}

此过程可以帮助嵌套关系,但是如果您使主页的网址更深,则必须修改分段数量。

@haakym $ show = Show :: find(1); 没有真正帮助,因为我试图自动获取id不是手动。我知道它的假设是有帮助的,但这个例子没有帮助