jeudi 30 mai 2019

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire