Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Datatable ManyToMany relationship with multiple tables

Offer.php #model

use App\OfferCategory;
use App\OfferCountries;
use App\OfferCreative;
use App\OfferTools;
use App\OfferTraffic;

class Offer extends Model {
public function offer_countries() {
    return $this->belongsToMany(OfferCountries::class);
}

public function offer_categories() {
    return $this->belongsToMany(OfferCategory::class);
}

public function offer_creatives() {
    return $this->hasMany(OfferCreative::class);
}

public function offer_tools() {
    return $this->hasMany(OfferTools::class);
}

public function offer_traffic() {
    return $this->hasMany(OfferTraffic::class);
}

public function platforms() {
    return $this->hasMany(Platform::class);
}

}

OfferController.php

 public function getMediaData() {
//        $model = Offer::with('offer_traffic');
//        return DataTables::eloquent($model)
//                        ->addColumn('traffic', function (Offer $user) {
//                            return $user->offer_traffic->map(function($post) {
//                                        return str_limit($post->allowed_traffic, 30, '...');
//                                    })->implode('<br>');
//                        })
//                        ->toJson();

        return datatables(DB::table('offers'))->toJson();
    }

I want to use all relation table given in offer.php and image in data table. I have tried with commented code in the controller but not able to get please help me to know where I'm doing wrong.

OfferCountries.php #model

use App\Offer;

class OfferCountries extends Model {

   function offers() {
         return $this->belongsToMany(Offer::class);
    }
}

 Database schema

like image 589
Tarun Sharma Avatar asked May 31 '19 06:05

Tarun Sharma


People also ask

What is many to many relationship in Laravel?

Laravel Many To Many Relationship relates a record on the table to one or many records in another table and vice versa. Unlike Laravel One to Many and One to One, the key is a pivot table in this relationship. The pivot table is a table where you define between two tables to link them.

How to get data with relationship DataTable in Laravel app?

If you need to use get data with relationship datatable in laravel app. In this example, i will give you step by step explanation datatables with relationship example laravel application. In First step, We need to get fresh laravel version application using bellow command. So Let's open terminal and run bellow command.

What is a pivot table in Laravel one to many?

Unlike Laravel One to Many and One to One, the key is a pivot table in this relationship. The pivot table is a table where you define between two tables to link them. A pivot table allows the relationship id from one model to be related to many other models and vice-versa. One real-life example we can think of is products and categories.

How do you define a many to many relationship in SQL?

The key in many to many relationship is the join (or pivot) table. The pivot table allows the relationship id from one model to be related to many other models and vice-versa. Many-to-many relationships are defined by writing a method that returns the result of the belongsToMany. In our example, we will define the two models.


1 Answers

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.

like image 121
user7747472 Avatar answered Oct 09 '22 13:10

user7747472