0

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!

Jon McIntosh
  • 1,263
  • 8
  • 14
  • I'm not sure what the problem is - are you getting an error message? There's nothing in the code you've given describing the relationships - try http://laravel.com/docs/eloquent#relationships? That's a good place to start. – Kryten Apr 10 '14 at 18:24
  • My question is the best way to create this relationship in Laravel 4. How do I build the models? I apologize if this is a very low-level question. – Jon McIntosh Apr 10 '14 at 18:37
  • Read the link given by Kryten, then you will know that example call you wrote above will not work. Setup relationships as described in docs and come back if you have further questions. – Jarek Tkaczyk Apr 10 '14 at 19:04
  • I updated my question with some more details. – Jon McIntosh Apr 10 '14 at 20:14

1 Answers1

0

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.

Jon McIntosh
  • 1,263
  • 8
  • 14