Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

Hi, I'm very interested in this.

Last updated 2 years ago.
0

Hi,

You need to create your own Pivot class. Then that pivot class could have a relation (account) that will return a HasOne relation.

http://laravel.com/docs/eloquent#working-with-pivot-tables

I've just done it ! I can post my code if you want.

Last updated 2 years ago.
0

SebSept said:

Hi,

You need to create your own Pivot class. Then that pivot class could have a relation (account) that will return a HasOne relation.

http://laravel.com/docs/eloquent#working-with-pivot-tables

I've just done it ! I can post my code if you want.

Thank you SebSept,

Please, post your code.

thanks in advance.

Last updated 2 years ago.
0

Pivot table is user_id, party_id, registration_status_id.

I wish to the player's RegistrationStatus when I query player on Party. (Party->players[x]->registrationStatus)

Models - Party (game)

namespace SebSept\PokerEventRegistry\Models;

use SebSept\PokerEventRegistry\Models\User;
use Eloquent;

class Party extends PartyPlayer { 

        /**
         * Players registred to $this Party
         * 
         * @return BelongsToMany
         */
	public function players()
	{
            return $this->belongsToMany('SebSept\PokerEventRegistry\Models\User')->withPivot('registration_status_id');
	}
        
}

Notice it extends PartyPlayer. PartyPlayer is a base model used for the both elements of the relation (Party + User) (otherwise it won't work)

class User extends PartyPlayer implements UserInterface, RemindableInterface {
}

Extends PartyPlayer.

namespace SebSept\PokerEventRegistry\Models;
use Eloquent;

class PartyPlayer extends Eloquent 
{
	
        public function newPivot(\Illuminate\Database\Eloquent\Model $parent, array $attributes, $table, $exists)
        {
            return new PartyPlayerRegistrationStatusPivot($parent, $attributes, $table, $exists);
        }
}

Our base model which implements newPivot . This Pivot will be return instead of the one generated by laravel.

namespace SebSept\PokerEventRegistry\Models;
use Eloquent;

class PartyPlayerRegistrationStatusPivot extends \Illuminate\Database\Eloquent\Relations\Pivot
{
    /**
    * The database table used by the model.
    *
    * @var string
    */
    protected $table = 'party_user';  
    
    public function registrationStatus()
    {
        return $this->hasOne('SebSept\PokerEventRegistry\Models\RegistrationStatus','id', 'registration_status_id');
    }
    
}

You can now have

$party->players->first()->pivot->registrationStatus

now return the registration status of the player, related to this $party

I did not test that the data are the one I want ! I just test it returns the right Model.

So now this test pass

    public function testPivot()
    {
        $reg_status = $this->model->players->first()
                ->pivot
                ->registrationStatus;
        $this->assertInstanceOf('SebSept\PokerEventRegistry\Models\RegistrationStatus', $reg_status);
    }
Last updated 2 years ago.
0

Wow!

Thank you SebSept, I really appreciate it. Exactly what I was looking for.

How can we mark this as a Solution?

Last updated 2 years ago.
0

This is great, but how would this then work with multiple pivot tables? Surely this solution would make it so all pivot relations return the same pivot model.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

MacWorks macworks Joined 6 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.