Level 20
As your pivot table don't have the "convention" name, you should specify it in your relation. And the related column too.
https://laravel.com/docs/5.6/eloquent-relationships#has-many-through Last example, before polymorphic relations
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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');
}
}
Please or to participate in this conversation.