Pregunta

I'm trying to make a mini CMS-type component and I have three models:

Page.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");
    }
}

Content.php

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

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

Edit: I've set up my relationship models with the correct relationships.

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

Returns the PageGroup collection belonging to the Page. I guess my new question is if it's possible to get a collection from that collection... Something like:

$content = $page_group->content;

Because that returns:

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

Does this make any sense? I apologize for the newbie question; I'm just getting into Laravel!

¿Fue útil?

Solución

I feel like an idiot.

The method chain $page->groups()->where("id", "=", 1)->get()->content; will not work because $page->groups()->where("id", "=", 1); returns a list of rows!

$page->groups()->where("id", "=", 1)->first()->content will work.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top