Laravel Datatable ManyToMany与多个表的关系

时间:2019-05-31 06:48:30

标签: php mysql laravel laravel-5 datatable

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();
    }

我想使用offer.php中提供的所有关系表和数据表中的图像。我已经尝试在控制器中使用带注释的代码,但是无法获取,请帮助我知道我在哪里做错了。

OfferCountries.php #model

使用App \ Offer;

class OfferCountries extends Model {

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

 Database schema

3 个答案:

答案 0 :(得分:2)

这就是我要怎么做,

在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');
    }
  }

在OfferOfferCountries.php

class OfferOfferCountries extends Model {
   public function countryDetail(){
     return $this->belongsTo(OfferCountries::class,'offercountry_id','id');
   }
}

在OfferOfferCategory.php

class OfferOfferCategory extends Model {
   public function categoryDetail(){
     return $this->belongsTo(OfferCategory::class,'offercategory_id','id');
   }
}

现在在控制器中

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);

}

这应该为您提供所有对象的数组。您可以使用数据透视表,但我喜欢这种方式。

答案 1 :(得分:0)

我认为Laravel由于数据库中的字段不标准(在许多情况下,offer_id而不是默认的主键),因此无法自动处理您的关系

要解决此问题,您必须手动提供必须在其上建立关系的字段名称。

Offer.php

class Offer extends Model
{
    public function offer_countries()
    {
        // Here we provide parent key field name (offer_id) because we don't want to use the primary key for that
        return $this->belongsToMany(OfferCountries::class, null, null, null, 'offer_id');
    }

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

    public function offer_tools()
    {
        // Map foreign key to local one 
        return $this->hasMany(OfferTools::class, 'offer_id', 'offer_id');
    }

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

    //...
}

最后一个模型是OfferCountriesoffers关系。我们必须提供relatedKey参数来告诉Eloquent在哪个字段中寻找外键:

OfferCountries.php

class OfferCountries extends Model
{
    function offers()
    {
        return $this->belongsToMany(Offer::class, null, null, null, null, 'offer_id');
    }
}

相关文档

https://laravel.com/docs/5.8/eloquent-relationships#one-to-many

https://laravel.com/docs/5.8/eloquent-relationships#many-to-many

答案 2 :(得分:0)

您没有遵循标准的命名约定,因此必须显式传递那些相关的键和表名。

Offer.php

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, 'offer_offer_countires', 'offer_id', 'offer_countries_id');
 }

 public function offer_categories() {
     return $this->belongsToMany(OfferCategory::class, 'offer_offer_categories', 'offer_id', 'offer_category_id');
 }

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

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

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

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

OfferCountries.php

use App\Offer;

class OfferCountries extends Model {

   function offers() {
         return $this->belongsToMany(Offer::class, 'offer_offer_countires', 'offer_countries_id', 'offer_id');
    }
}