Php 拉威尔雄辩的关系有很多原因

Php 拉威尔雄辩的关系有很多原因,php,laravel,eloquent,relationship,laravel-5.1,Php,Laravel,Eloquent,Relationship,Laravel 5.1,我正试图了解拉威尔的关系,但在我试图实现的最后一部分上有困难 我的桌子是这样的: 页面模板 id id pagetemplate_id id pagetemplate_id 页面 id id pagetemplate_id id pagetemplate_id pagetemplate\u块 id id pagetemplate_id id pagetemplate_id pagetemplateblocks\u内容 id page_id page_template_block

我正试图了解拉威尔的关系,但在我试图实现的最后一部分上有困难

我的桌子是这样的:

页面模板

id
id
pagetemplate_id
id
pagetemplate_id
页面

id
id
pagetemplate_id
id
pagetemplate_id
pagetemplate\u块

id
id
pagetemplate_id
id
pagetemplate_id
pagetemplateblocks\u内容

id
page_id
page_template_block_id
所以,当从DB中加载页面时,我需要使用页面模板、页面块和内容来获取页面

以下是我目前的代码:

Page.php

public function pageTemplate()
{
    return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
}

public function pagetemplateblockcontent()
{
    return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblocks_content', 'page_id', 'page_template_block_id')->withTimestamps();
}

public function pagecontent()
{
    return $this->hasMany('PageTemplateBlockContent', 'page_id')->with('pagetemplateblock');
}
public function page()
{
    return $this->hasMany('Page', 'pagetemplate_id');
}    

public function blocks() {
    return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
}
public function pagetemplate() {
    return $this->belongsTo('PageTemplate', 'pagetemplate_id');
}

public function blockcontent() {
    return return $this->hasOne('PageTemplateBlockContent');
}
public function pagetemplateblock()
{
    return $this->belongsTo('PageTemplateBlock', 'page_template_block_id');
}
PageTemplate.php

public function pageTemplate()
{
    return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
}

public function pagetemplateblockcontent()
{
    return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblocks_content', 'page_id', 'page_template_block_id')->withTimestamps();
}

public function pagecontent()
{
    return $this->hasMany('PageTemplateBlockContent', 'page_id')->with('pagetemplateblock');
}
public function page()
{
    return $this->hasMany('Page', 'pagetemplate_id');
}    

public function blocks() {
    return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
}
public function pagetemplate() {
    return $this->belongsTo('PageTemplate', 'pagetemplate_id');
}

public function blockcontent() {
    return return $this->hasOne('PageTemplateBlockContent');
}
public function pagetemplateblock()
{
    return $this->belongsTo('PageTemplateBlock', 'page_template_block_id');
}
PageTemplateBlock.php

public function pageTemplate()
{
    return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
}

public function pagetemplateblockcontent()
{
    return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblocks_content', 'page_id', 'page_template_block_id')->withTimestamps();
}

public function pagecontent()
{
    return $this->hasMany('PageTemplateBlockContent', 'page_id')->with('pagetemplateblock');
}
public function page()
{
    return $this->hasMany('Page', 'pagetemplate_id');
}    

public function blocks() {
    return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
}
public function pagetemplate() {
    return $this->belongsTo('PageTemplate', 'pagetemplate_id');
}

public function blockcontent() {
    return return $this->hasOne('PageTemplateBlockContent');
}
public function pagetemplateblock()
{
    return $this->belongsTo('PageTemplateBlock', 'page_template_block_id');
}
PageTemplateBlockContent.php

public function pageTemplate()
{
    return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
}

public function pagetemplateblockcontent()
{
    return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblocks_content', 'page_id', 'page_template_block_id')->withTimestamps();
}

public function pagecontent()
{
    return $this->hasMany('PageTemplateBlockContent', 'page_id')->with('pagetemplateblock');
}
public function page()
{
    return $this->hasMany('Page', 'pagetemplate_id');
}    

public function blocks() {
    return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
}
public function pagetemplate() {
    return $this->belongsTo('PageTemplate', 'pagetemplate_id');
}

public function blockcontent() {
    return return $this->hasOne('PageTemplateBlockContent');
}
public function pagetemplateblock()
{
    return $this->belongsTo('PageTemplateBlock', 'page_template_block_id');
}
但是,问题在于
内容
,如果我尝试此操作,它将返回一个PageTemplateBlockContent实例,这对于所有页面都是相同的。尽管每个页面应该有不同的PageTemplateBlockContent。我不知道该如何解决这个问题,所以任何想法都将不胜感激

已更新

我的控制器呼叫

$this->pageRepo->where('id', $id)->with('pageTemplate');
“pageTemplate”返回以下内容:

return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
return $this->hasOne('PageTemplateBlockContent');
“块”返回以下内容:

return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
return $this->hasOne('PageTemplateBlockContent');
“blockcontent”返回以下内容:

return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');
return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');
return $this->hasOne('PageTemplateBlockContent');
因此,这意味着它永远不会点击Joost建议创建的“pagetemplateblockcontent”。

更改

public function pagetemplateblockcontent()
{
    return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblocks_content', 'page_id', 'page_template_block_id')->withTimestamps();
}

并创建一个带有两个主键的表page_pagetemplateblockcontent,如下所示

$table->integer("page_id")->unsigned();
$table->integer("page_template_block_id")->unsigned();

$table->primary(['page_id', 'page_template_block_id']);

你的查询/获取数据的方法是什么?我正在使用('pageTemplate')的
$this->pageRepo->where('id',$id)->
这是怎么回事?
$this->pageRepo
请在你的问题中添加完整的方法这不是一个方法,它实例化了一个有说服力的存储库,这样我就可以访问有说服力的方法(其中,使用等)。唯一的一点(我感到困惑的部分)是一个pagetemplateblock可以有许多pagetemplateblock内容,尽管这与特定页面以及pagetemplateblock相关。您的示例是否考虑到了这一点?如果您将页面链接到pagetemplateblockcontent,您只会获得与页面的链接关系。是的,如果你是这个意思。保持代码简单页面不必直接链接pagetemplateblock或其内容如果关系设置正确,则应获取仅与当前页面相关的内容。我刚刚尝试了您的方法,但收到以下错误:
Relationship方法必须返回类型为的对象Illumb\Database\Elount\Relations\Relations
编辑了我的awnser您是这样设置关系的吗?我已经更改了模型以反映您的更改Joost,现在我有一个错误:
调用未定义的方法Illumb\Database\Query\Builder::pageTemplateBlocks()
尽管PageTemplate.php中肯定存在此方法,有什么想法吗?非常感谢你的帮助。