Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 4 months ago.
0

Improvement :

class Party extends Eloquent {

public function players()
{
	$relation = $this->belongsToMany('User')->withPivot('registration_status_id');
	$players = $relation->get();
	foreach($players as &$p) {
		$p->registration_status = RegistrationStatus::find( (int) $p->pivot->registration_status_id );
	}

	return $players;
}
}

but function still returns a collection, not a Relation object. So still can't use Party::find(2)->players[0]->registration_status->name; but Party::find(2)->players()[0]->registration_status->name

Last updated 4 months ago.
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

SebSept sebsept Joined 20 Feb 2014

Moderators

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

Your logo here?

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

© 2022 Laravel.io - All rights reserved.