簡體   English   中英

Laravel Eloquent的longestToMany和hasMany關系查詢

[英]Laravel Eloquent belongsToMany and hasMany relationship query

我正在使用Laravel 4.2.8。

我有兩個雄辯的模型: RapidsPacketRapidsQuestion RapidsPacket可以包含任意數量的RapidsQuestionRapidsQuestion可以位於任意數量的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');
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM