Laravel Datatable ManyToMany relationship with multiple tables
Here is how i would do,
In Offer.php
class Offer extends Model {
public function offer_countries() {
return $this->hasMany(OfferOfferCountries::class,'offer_id','id');
}
public function offer_categories() {
return $this->hasMany(OfferOfferCategories::class,'offer_id','id');
}
public function offer_creatives() {
return $this->hasMany(OfferCreative::class,'offer_id','id');
}
public function offer_tools() {
return $this->hasMany(OfferTools::class,'offer_id','id');
}
public function offer_traffic() {
return $this->hasMany(OfferTraffic::class,'offer_id','id');
}
public function platforms() {
return $this->hasMany(Platform::class,'offer_id','id');
}
}
In OfferOfferCountries.php
class OfferOfferCountries extends Model {
public function countryDetail(){
return $this->belongsTo(OfferCountries::class,'offercountry_id','id');
}
}
In OfferOfferCategory.php
class OfferOfferCategory extends Model {
public function categoryDetail(){
return $this->belongsTo(OfferCategory::class,'offercategory_id','id');
}
}
Now in the controller
public function getMediaData() {
$data = Offer::with('offer_countries.countryDetail','offer_categories.categoryDetail','offer_creatives','offer_tools','offer_traffic','platforms')->get();
echo '<pre>';
print_r($data);
}
This should give you an array of objects of everything. You can use pivot table but I like this way.