drfif48428 2014-08-22 11:37
浏览 31
已采纳

Laravel Eloquent属于ToMany并且有很多关系查询

I'm using Laravel 4.2.8.

I have two Eloquent models: RapidsPacket and RapidsQuestion. A RapidsPacket can contain any number of RapidsQuestion and a RapidsQuestion may be in any number of RapidsPacket.

Here are my database tables:

For the RapidsPacket model:

packets
 - id
 - name

For the RapidsQuestion model:

rapids
 - id
 - question

And the pivot table:

packet_rapid
 - id
 - packet_id
 - rapid_id

Here is the current skeleton of the models:

class RapidsPacket extends Eloquent {

    protected $table = 'packets';

    /**
     * Get this packet's questions
     *
     * @return RapidsQuestion
     */     
    public function questions()
    {
        return $this->belongsToMany('RapidsQuestion');
    }

}

and

class RapidsQuestion extends Eloquent {

    protected $table = 'rapids';

    /**
     * Get this question's packets
     *
     * @return RapidsPacket
     */     
    public function packets()
    {
        return $this->belongsToMany('RapidsPackets');
    }

}

What I want to be able to do is grab all questions in a packet, e.g:

$packet = RapidsPacket::find(2);
$my_questions = $packet->questions()->get();

I also want to be able to get the packets, that a question might belong to. E.g:

$question = RapidsQuestion::find(30);
$my_packets = $question->packets()->get();

If I try either of the above lines of code and var_dump() either $my_questions or $my_packets I get an enormous object containing all sorts of Laravel things but not the requested data.

I have tried various iterations of passing different foreign and local keys to the belongsToMany() function but it never seems to return any models. This is driving me crazy. I've read countless answers on SO and the wider net about this problem but 5 hours down the line and I'm still no further. Please help!

Update 2:

Here is the contents of my database tables (v small):

packets

id: 1
name: test

rapids

id: 30
question: 'sample 1'

id: 40
question: 'sample 2'

id: 50
question: 'sample 3'

_packet_rapid_

id: 1
packet_id: 1
rapid_id: 30

id: 2
packet_id: 1
rapid_id: 40

id: 3
packet_id: 1
rapid_id: 50

Update 1:

Just changed RapidsPacket to belongsToMany rather than hasMany but still to no avail

  • 写回答

2条回答 默认 最新

  • doushaizhen1244 2014-08-22 12:22
    关注

    I think it must be:

    class RapidsQuestion extends Eloquent {
    
    protected $table = 'rapids';
    
    /**
     * Get this question's packets
     *
     * @return RapidsPacket
     */     
    public function packets()
    {
        return $this->belongsToMany('RapidsPacket', 'packet_rapid', 'rapid_id', 'packet_id');
    }
    
    }
    

    .

    class RapidsPacket extends Eloquent {
    
    protected $table = 'packets';
    
    /**
     * Get this packet's questions
     *
     * @return RapidsQuestion
     */     
    public function questions()
    {
        return $this->belongsToMany('RapidsQuestion', 'packet_rapid', 'packet_id', 'rapid_id');
    }
    
    }
    

    I think you should name class and table same, it make you code easier

    class Rapid extends Eloquent {
    
    protected $table = 'rapids';
    
    /**
     * Get this question's packets
     *
     * @return RapidsPacket
     */     
    public function packets()
    {
        return $this->belongsToMany('Packet', 'packet_rapid');
    }
    
    }
    

    .

    class Packet extends Eloquent {
    
    protected $table = 'packets';
    
    /**
     * Get this packet's questions
     *
     * @return RapidsQuestion
     */     
    public function questions()
    {
        return $this->belongsToMany('Rapid', 'packet_rapid');
    }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?