drbouzlxb92333332 2015-10-05 07:54
浏览 345

Laravel hasManyThrough但必须只返回一个数组

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.

  • 写回答

2条回答 默认 最新

  • dongyao1915 2015-10-07 07:40
    关注

    I think you should use 'foreign_key' with 'hasMany' and 'hasOne'.

    return $this->hasMany('Comment', 'foreign_key');

    class Blog extends Model
    {
     public function entries()
     {
        return $this->hasMany(Entry::class, 'blog_id');
     }
    
     public field_group()
     {
        return $this->hasOne(FieldGroup::class, 'blog_id');
     }
    }
    

    refer http://laravel.com/docs/4.2/eloquent#one-to-many

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测