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

anuj1992's avatar

Pivot Table

I am trying to create pivot table of three different tables, I donkt know why it is not working, I have as following

          <?php namespace App\Models;

            use Illuminate\Database\Eloquent\Model;

           class Rewards extends Model {

           protected $table = 'rewards';

           public function campaign()
          {
           return $this->belongsToMany('App\Models\Campaign');
          }

          public function players()
         {
           return $this->belongsToMany('App\Models\Players');
       }

       public function rewardcampaignfundraisinggroup()
        {
             return $this->hasMany('App\Models\RewardCardCampaignPlayers');
     }

   }

Campaign.php

     <?php namespace App\Models;


        use Illuminate\Database\Eloquent\Model;
       use Illuminate\Database\Eloquent\SoftDeletes;
     use App\Helpers\SluggableTrait;

    class Campaign extends Model
    {
   use SoftDeletes;
   use SluggableTrait;
    protected $table = 'campaigns';
   protected $dates = ['deleted_at'];

public function players()
    {
    return $this->belongsToMany('App\Models\Players');
   }

     public function rewards()
   {
      return $this->belongsToMany('App\Models\Rewards');
    }

}

Players.php

 <?php namespace App\Models;


  use Illuminate\Database\Eloquent\Model;
  use Illuminate\Database\Eloquent\SoftDeletes;
  use App\Helpers\SluggableTrait;

   class  Players extends Model
   {
     use SoftDeletes;
      use SluggableTrait;

        protected $table = 'players';
       protected $dates = ['deleted_at'];

      public function campaigns()
       {
       return $this->belongsToMany('App\Models\Campaign');
       }

       public function rewards()
      {
          return $this->belongsToMany('App\Models\Rewards');
     }

  }

RewardsCanoaignPlayers.php

  <?php namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

   class RewardCardCampaignFundraisingGroup extends Model {

/**
 * The database table used by the model.
 *
 * @var string
 */
         protected $table = 'reward_campaign_player';

         public $timestamps = false;


       public function rewards()
        {
           return $this->belongsTo('App\Models\Rewards');
    }
     public function campaign()
     {
       return $this->belongsTo('App\Models\Campaign', 'campaign_id','id');
    }

      public function fg_player(){
     return $this->belongsTo('App\Models\Players', 'players_id','id');
     }
}
0 likes
1 reply

Please or to participate in this conversation.