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.
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.
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);
}
Wow!
Thank you SebSept, I really appreciate it. Exactly what I was looking for.
How can we mark this as a Solution?
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community