Laravel Eloquent belongsToMany和hasMany关系查询


Laravel Eloquent belongsToMany and hasMany relationship query

我使用的是Laravel 4.2.8。

我有两个Eloquent模型:RapidsPacketRapidsQuestion。一个RapidsPacket可以包含任意数量的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();

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

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

如果我尝试上述任何一行代码和var_dump()$my_questions$my_packets,我得到一个巨大的对象包含各种各样的Laravel的东西,但不是请求的数据。

我尝试了传递不同的外键和本地键到belongsToMany()函数的各种迭代,但它似乎从未返回任何模型。这快把我逼疯了。我在SO和更广泛的网络上读过无数关于这个问题的答案,但5个小时过去了,我仍然没有进一步的答案。请帮助!

更新2:

下面是我的数据库表的内容(v小):

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,但仍然无济于事

我想一定是:

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

belongsToMany的反义词是belongsToMany

当使用hasMany();它暗示目标与父对象有一个belongsTo关系。

总结:

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