Garry Pettet
Garry Pettet

Reputation: 8290

Laravel Eloquent belongsToMany and hasMany relationship query

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

Upvotes: 1

Views: 5578

Answers (2)

Trung
Trung

Reputation: 650

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

}

Upvotes: 5

Pinoniq
Pinoniq

Reputation: 1385

The oposite of belongsToMany is belongsToMany

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

To sum up:

class RapidsPacket extends Eloquent {

    protected $table = 'packets';

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

}

Upvotes: 0

Related Questions