Laravel Eloquent 的belongsToMany 和hasMany 关系查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25446092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:58:44  来源:igfitidea点击:

Laravel Eloquent belongsToMany and hasMany relationship query

phplaravellaravel-4

提问by Garry Pettet

I'm using Laravel 4.2.8.

我正在使用 Laravel 4.2.8。

I have two Eloquent models: RapidsPacketand RapidsQuestion. A RapidsPacketcan contain any number of RapidsQuestionand a RapidsQuestionmay be in any number of RapidsPacket.

我有两个 Eloquent 模型:RapidsPacketRapidsQuestion. ARapidsPacket可以包含任意数量的RapidsQuestion并且 a可以包含RapidsQuestion任意数量的RapidsPacket

Here are my database tables:

这是我的数据库表:

For the RapidsPacket model:

对于 RapidsPacket 模型:

packets
 - id
 - name

For the RapidsQuestion model:

对于 RapidsQuestion 模型:

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_questionsor $my_packetsI get an enormous object containing all sorts of Laravel things but not the requested data.

如果我尝试无论是上面的代码行和var_dump()任一$my_questions$my_packets我得到一个包含各种Laravel事情,但没有请求的数据巨大的对象。

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!

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

Update 2:

更新 2:

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

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

packets

数据包

id: 1
name: test

rapids

急流

id: 30
question: 'sample 1'

id: 40
question: 'sample 2'

id: 50
question: 'sample 3'

_packet_rapid_

_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:

更新 1:

Just changed RapidsPacket to belongsToManyrather than hasManybut still to no avail

只是将 RapidsPacket 改为belongsToMany而不是hasMany但仍然无济于事

回答by Lê Tr?n Ti?n Trung

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

}

回答by Pinoniq

The oposite of belongsToMany is belongsToMany

belongsToMany的oposite是belongsToMany

When using hasMany(); it implies that the target has a belongsTo relation to the parent.

当使用 hasMany(); 这意味着目标与父对象有一个属于关系。

To sum up:

总结:

class RapidsPacket extends Eloquent {

    protected $table = 'packets';

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

}