【发布时间】:2020-03-06 03:33:43
【问题描述】:
我想将我的集合数组键更改为语言代码。
这是我的代码
$products = Product::has('languages')->paginate(20);
return response()->json($products);
这是我的关系
public function languages($lang_id = null)
{
if ($lang_id)
return $this->hasMany(ProductLang::class)->where('lang_id', $lang_id)->first();
return $this->hasMany(ProductLang::class);
}
这是我现在得到的。
Illuminate\Database\Eloquent\Collection {#613 ▼
#items: array:2 [▼
0 => App\Models\Product\ProductLang {#614 ▶}
1 => App\Models\Product\ProductLang {#615 ▶}
]
}
预期:
Illuminate\Database\Eloquent\Collection {#613 ▼
#items: array:2 [▼
en => App\Models\Product\ProductLang {#614 ▶}
zh => App\Models\Product\ProductLang {#615 ▶}
]
}
感谢您的帮助..
【问题讨论】:
-
您能否澄清一下您是如何获得
ProductLang的集合的,因为您的代码显示您正在查询Product -
@Helioarch 我正在使用急切加载获取产品是的
-
感谢您的澄清,我在下面提供了我的答案
标签: php laravel laravel-6 laravel-collection