【问题标题】:Laravel Eloquent Relationship hasMany hasOneLaravel Eloquent 关系 hasMany hasOne
【发布时间】:2016-03-17 05:23:27
【问题描述】:

我正在努力了解 Laravel 的关系,但对我要实现的最后一部分有困难。

我的桌子是这样的:

页面模板

id

页面

id
pagetemplate_id

pagetemplate_blocks

id
pagetemplate_id

pagetemplateblocks_content

id
page_id
page_template_block_id

因此,当从数据库中加载页面时,我需要使用页面模板、块和内容来获取它。

到目前为止,这是我的代码:

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');
}

PageTemplate.php

public function page()
{
    return $this->hasMany('Page', 'pagetemplate_id');
}    

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

PageTemplateBlock.php

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

public function blockcontent() {
    return return $this->hasOne('PageTemplateBlockContent');
}

PageTemplateBlockContent.php

public function pagetemplateblock()
{
    return $this->belongsTo('PageTemplateBlock', 'page_template_block_id');
}

但是,问题在于content,如果我尝试这样做,它会返回一个 PageTemplateBlockContent 实例,这对于所有页面都是相同的。虽然每个 Page 应该有不同的 PageTemplateBlockContent。我不知道如何解决这个问题,所以任何想法都将不胜感激。

更新

我的控制器调用

$this->pageRepo->where('id', $id)->with('pageTemplate');

“pageTemplate”返回以下内容:

return $this->belongsTo('PageTemplate', 'pagetemplate_id')->with('blocks');

“blocks”返回以下内容:

return $this->hasMany('PageTemplateBlock', 'pagetemplate_id')->orderBy('sort_order', 'asc')->with('blockcontent');

“blockcontent”返回以下内容:

return $this->hasOne('PageTemplateBlockContent');

所以这意味着它永远不会碰到 Joost 建议创建的“pagetemplateblockcontent”。

【问题讨论】:

  • 您的查询/获取数据的方式是什么?
  • 我正在使用$this->pageRepo->where('id', $id)->with('pageTemplate');
  • $this->pageRepo 是什么,请将完整方法添加到您的问题中
  • 它不是一个方法,它实例化一个 eloquent 存储库,以便我可以访问 eloquent 方法(where、with 等)

标签: php laravel eloquent relationship laravel-5.1


【解决方案1】:

改变

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

public function pagetemplateblockcontent()
{
    return $this->belongsToMany('PageTemplateBlockContent', 'pagetemplateblockscontent_page', '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']);

【讨论】:

  • 唯一(我感到困惑的部分)是一个页面模板块可以有许多页面模板块内容,尽管这与特定页面以及页面模板块有关。您的示例是否考虑到了这一点?
  • 如果您将页面链接到 pagetemplateblockcontent 您只会获得与页面链接的关系。是的,如果你是这个意思。保持代码简单 如果您的关系设置正确,页面不必直接与 pagetemplateblock 或其内容链接,您将获得仅与当前页面相关的那些。
  • 我刚试过你的方法,但收到以下错误:Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
  • Edited my awnser 你是这样设置关系的吗?
  • 我已经更改了模型以反映您的更改 Joost,现在我有这个错误:Call to undefined method Illuminate\Database\Query\Builder::pageTemplateBlocks() 虽然这个方法肯定存在于 PageTemplate.php 中,有什么想法吗?非常感谢您对此提供的帮助。
猜你喜欢
  • 2020-01-08
  • 2020-03-05
  • 2020-11-26
  • 2020-01-18
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 2020-03-04
  • 2023-01-12
相关资源
最近更新 更多