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

时间:2014-08-22 11:37:13

标签: php laravel laravel-4

我正在使用Laravel 4.2.8。

我有两个Eloquent模型:RapidsPacketRapidsQuestionRapidsPacket可以包含任意数量的RapidsQuestion,而RapidsQuestion可以包含任意数量的RapidsPacket

以下是我的数据库表:

对于RapidsPacket模型:

packets
 - id
 - name

对于RapidsQuestion模型:

rapids
 - id
 - question

枢轴表:

packet_rapid
 - id
 - packet_id
 - rapid_id

以下是模型的当前骨架:

class RapidsPacket extends Eloquent {

    protected $table = 'packets';

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

}

class RapidsQuestion extends Eloquent {

    protected $table = 'rapids';

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

}

我希望能够做的是抓住数据包中的所有问题,例如:

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

我还希望能够获得一个问题可能属于的数据包。 E.g:

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

如果我尝试上述任一行代码而var_dump() $my_questions$my_packets,我会得到一个包含各种Laravel内容的巨大对象,但不包含所请求的数据。

我尝试过将不同的外键和本地键传递给belongsToMany()函数的各种迭代,但它似乎永远不会返回任何模型。这真让我抓狂。我已经在SO和更广泛的网络上阅读了关于这个问题的无数答案,但是5个小时下线,我仍然没有进一步。请帮忙!

更新2:

以下是我的数据库表(v small)的内容:

分组

id: 1
name: test

急流

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

更新1:

刚刚将RapidsPacket更改为belongsToMany而不是hasMany,但仍无济于事

2 个答案:

答案 0 :(得分:5)

我认为一定是:

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');
}

}

我认为你应该将类和表命名相同,这会让你的代码更容易

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 :(得分:0)

oposite of belongsToMany is belongsToMany

使用hasMany()时;它表示目标与父项具有belongsTo关系。

总结一下:

class RapidsPacket extends Eloquent {

    protected $table = 'packets';

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

}
相关问题