laravel中的别名列名称雄辩

时间:2017-07-13 05:06:17

标签: php laravel-5.2

我正在使用Laravel 5.2,我有如下的Eloquent:

return $this->with('useType')->with('distributionChannelType')
                    ->with('commercialModelType')->with('deliveryChannel')
                    ->with('territoryCode')->with('mobileOperatorCode')->with('serviceCode')
                    ->where('content_partner_id', $cp_id)->get()->toArray();

这将返回如下数据:

[
{
"id": 36,
 "created_at": "2017-07-12 08:43:13",
 "usetype": [
{
"revenue_share_id": 36,
 "use_type_id": 39,
 "use_type": "UseAsRingbackTune",
 "definition": "To Use a Resource in a Release as ringbacktune."
},
 {
"revenue_share_id": 36,
 "use_type_id": 31,
 "use_type": "Stream",
 "definition": "To stream a Resource"
}
],
 "commercialmodeltypes": [
{
"revenue_share_id": 36,
 "commercial_model_type_id": 5,
 "commercial_model_type": "PayAsYouGoModel"
}
],
 "deliverychannels": [
{
"revenue_share_id": 36,
 "delivery_channel_id": 1,
 "delivery_channel": "Desktop App"
}
],
 "territorycodes": [
{
"revenue_share_id": 36,
 "territory_code_id": 250,
 "revenue_share": "43.00",
 "territory_code": "Africa"
}
],
 "mobileoperatorcodes": [
{
"revenue_share_id": 36,
 "mobile_operator_code_id": 2,
 "revenue_share": "43.00",
 "mobile_operator_code": "Benin (Glo Mobile)"
}
],
 "servicecodes": [
{
"revenue_share_id": 36,
 "service_code_id": 7,
 "revenue_share": "87.00",
 "service_code": "Artist Pack"
}
],
 "languagecodes": [
{
"revenue_share_id": 36,
 "language_code_id": 3,
 "revenue_share": "0.00",
 "language_code": "French(fr)"
}
]
}
]

在这个数据中我想要别名列名,例如:

use_type_id AS id

use_type AS标签

我该怎么做?我感谢所有的回应。谢谢。

1 个答案:

答案 0 :(得分:1)

return $this->with('useType AS id or any name you want to give')->with('distributionChannelType')
                    ->with('commercialModelType')->with('deliveryChannel')
                    ->with('territoryCode')->with('mobileOperatorCode')->with('serviceCode')
                    ->where('content_partner_id', $cp_id)->get()->toArray();

试试这个

public function useType() { return $this->belongsToMany('App\UseType', 'UsageRightUseTypes')->withPivot('usage_right_id', 'use_type_id as some_name'); }