0

我正在尝试制作一个迷你 CMS 类型的组件,我有三个模型:

页面.php

class Page extends Eloquent {
    protected $table = 'pages';

    public function content() {
        return $this->hasManyThrough('Content', 'PageGroup');
    }

    public function groups() {
        return $this->hasMany('PageGroup');
    }
}

PageGroup.php

class PageGroup extends Eloquent {
    protected $table = 'page_groups'

    public function group() {
        return $this->hasMany("Content");
    }
}

内容.php

class Content extends Eloquent {
    protected $table = 'content';

    public function content() {
        return $this->hasMany("Content");
    }
}

编辑:我已经用正确的关系建立了我的关系模型。

$page = Page::find(1);
$page_group = $page->groups()->where('id', '=', 1)->get();

返回PageGroup属于 的集合Page。我想我的新问题是是否有可能从该收藏中获得收藏......比如:

$content = $page_group->content;

因为那会返回:

Undefined property: Illuminate\Database\Eloquent\Collection::$content

这有道理吗?我为新手问题道歉;我刚刚进入 Laravel!

4

1 回答 1

0

我觉得自己像个白痴。

方法链$page->groups()->where("id", "=", 1)->get()->content;将不起作用,因为$page->groups()->where("id", "=", 1);返回行列表!

$page->groups()->where("id", "=", 1)->first()->content将工作。

于 2014-04-10T19:28:55.113 回答