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.