Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mrs_Beginner's avatar

select winners with their information from Pivot table with extra fields of users and matches tables

hi i have 3 table users table

matches table

match_user pivot table with extra fields ('user_id','match_id','user_rank','user_reward') and pivot table code is like below :


     Schema::create('match_user', function(Blueprint $table)
        {
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('NO ACTION');
            $table->integer('match_id')->unsigned()->index();
            $table->foreign('match_id')->references('id')->on('matches')->onDelete('NO ACTION');
            $table->integer('user_rank')->default(0);
            $table->string('user_reward')->default(0);
        });

i wanna select all winners with their information and match information, who is winner? when "user_rank" or "user_reward" value is not equal to "0",

and user model code is like below:

class User extends Authenticatable
{
    use Notifiable,HasApiTokens;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','mobile','api_token','imageurl'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token','api_token'
    ];

    //function fo relating to matches
    public function matches(){
        return $this->belongsToMany('App\match');
    }
}

and match model code is like below:

class match extends Model
{
    protected $fillable=['name','detail','price','deadline','matchtype','reward','imageurl'];

    //function to reffer to users
    public function users(){
        return $this->belongsToMany('App\User');
    }
}

so now how can i get winner information? with match name and winner rank and reward?

i know laravel have some magic way to handle pivot tables with extra field but i dont what is it and how it works.

so how can i get winners info? and if you have any idea to make my code more optimize please leave a comment,thanks.

0 likes
2 replies
D9705996's avatar

From your description in not sure how you determine a winner so can't give you a full answer but the following will print out each users name then their rank for a given match.

foreach ($match->users as $user) {
    echo $user . ' - ' . $user->pivot->user_rank;
}

The pivot relationship is automatically created by laravel magic. You can find more examples and info at https://laravel.com/docs/5.7/eloquent-relationships#many-to-many

1 like
bobbybouwmann's avatar
Level 88

You can find everything you need in the documentation.

Documentation: https://laravel.com/docs/5.7/eloquent-relationships#many-to-many

Small note! You need to add the extra pivot values to your relationships

public function users()
{
    return $this->belongsToMany('App\User')->withPivot('user_rank', 'user_reward');
}

Based on those relationships you can do some queries for it.

$match = Match::with('users')->find($matchId);

foreach ($match->users as $user) {
    if ($user->pivot->user_rank === 1) {
        echo 'Winner';
    } else {
        echo 'Loser';
    }
}
1 like

Please or to participate in this conversation.