Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel hasManyThrough but must return only one array

I have these tables:

Entries table

---------------------------------
| id  |  blog_id  |     title   |
---------------------------------
| 1   |      1     | 1st Entry  |
---------------------------------

Blogs Table

-----------------
| id  |   name  |
-----------------
|  1  | 1stBlog |
-----------------

Field Groups Table

-------------------------
| id | blog_id |  name  |
-------------------------
| 1  |    1    | Group1 |
-------------------------

Fields Table

---------------------------------
| id | field_group_id |  name   |
---------------------------------
| 1  |       1        | field_1 |
---------------------------------

Values Table

------------------------------------------
| id | field_id | entry_id |    value    |
------------------------------------------
| 1  |    1     |     1    | Hello World |
------------------------------------------

Now on my Models I've set up these relationships:

class Entry extends Model
{
    public function blog()
    {
        return $this->belongsTo(Blog::class);
    }
}

class Blog extends Model
{
    public function entries()
    {
        return $this->hasMany(Entry::class);
    }

    public field_group()
    {
        return $this->hasOne(FieldGroup::class);
    }
}

class FieldGroup extends Model
{
    public function fields()
    {
        return $this->hasMany(Entry::class);
    }

    public function blog()
    {
        return $this->belongsTo(Blog::class);
    }
}

class Field extends Model
{
    public function group()
    {
        return $this->belongsTo(FieldGroup::class, 'field_group_id');
    }

    public function values()
    {
        // this method should get the values from the Values table per entry id
        return $this->hasManyThrough(Value::class, Entry::class, 'id', 'entry_id');
    }
}

class Value extends Model
{
    public function field()
    {
        return $this->belongsTo(Field::class, 'field_id');
    }
}

Using this query I can

$entry = Entry::with('blog.field_group.fields')->find(1)

I can get the entry, along with its blog, field groups and fields. I want to get the values associated with the entry too,

$entry = Entry::with('blog.field_group.fields.values')->find(1)

I am having trouble on which relationship to use. Any help is much appreciated. I just started using laravel.

like image 464
sumpreto Avatar asked Oct 05 '15 07:10

sumpreto


1 Answers

Try it...

Replace ID by field_id:

return $this->hasManyThrough(Value::class, Entry::class, 'id', 'entry_id');

Like this:

return $this->hasManyThrough(Value::class, Entry::class, 'field_id', 'entry_id');

Because you are using the standard laravel column names you can simplify even more:

return $this->hasManyThrough(Value::class, Entry::class);

See in: https://laravel.com/docs/5.1/eloquent-relationships#has-many-through

like image 56
JuniorNunes Avatar answered Nov 08 '22 18:11

JuniorNunes