Daniel-Pablo's avatar

Want to short this, HELP!!

$user->rankDistributor->first()->affiliation_distributor_income

I want to write something like this and get the value :

$user->rankDistributor->affiliation_distributor_income
0 likes
8 replies
James_Moore's avatar

Just add a custom method on your user model that wraps the following code you have here.

Snapey's avatar

If you are having to use first then it probably means you have a hasMany or belongsToMany relationship where it should be singular

Daniel-Pablo's avatar

thanks @snapey I got this,

  public function rankDistributor()
  {
      return $this->belongsToMany('App\RankDistributor' , 'ranks' , 'user_id' , 'distributor' );
  }

That reference the code, there is something I could do?

Snapey's avatar

What is the relationship between User and RankDistributor

Daniel-Pablo's avatar

this is the relation on the USER model @snapey

  public function rankDistributor()
  {
      return $this->belongsToMany('App\RankDistributor' , 'ranks' , 'user_id' , 'distributor' );
  }

got a table called RANKS and other called RANK_DISTRIBUTOR the RANKS have the rank as client provider and distributor so it related as a hasOne rank

  public function rank()
  {
      return $this->hasOne( 'App\Rank');
  }

BUT when I want to get the information of the levels of the rank distributor the relation goes like , he is rank 3 on ranks then PIVOT THE INFO

this code is under the USER MODEL

so I call it like this way

$user->rankDistributor->first()->affiliation_distributor_income

I want to make a smaller call of it,.... it is possible?

Daniel-Pablo's avatar

@snapey @james_moore with PLUCK I get this

 return $user->rankProvider->pluck('service_income');
// give this 
[
60
]
// I want just the 60

what that but with out the [] ...

James_Moore's avatar

if its returning an array of just one element you could just

$user->rankProvider->pluck('service_income')[0];

I think but their is probably a better way to do this.

Snapey's avatar
Snapey
Best Answer
Level 122
return $user->rankProvider->first()->service_income;

Please or to participate in this conversation.