我正在使用 laravel 6 开发多语言 API,我的数据库中有这种情况:
Categories
id
other not relevants fields
Languages:
id
name
code
Category_Language
id
language_id
category_id
name --> this is the name of the category in the specific language.
现在我有 2 个模型,第一个用于类别
class Category extends Model
{
public function languages()
{
return $this->belongsToMany('App\Models\v1\Language')->withTimestamps()->withPivot('name');
}
}
和第二个模型
class Language extends Model
{
protected $fillable = ['code', 'name', 'image_id', 'enabled'];
public function categories() {
return $this->belongsToMany('App\Models\v1\Category')->withTimestamps()->withPivot('name');
}
}
在我的逻辑中(我正在使用服务模式),我在创建类别时使用了这种方法,每次我传递这样的 JSON 对象时:
{
"names": [
{
"languageId": 1,
"name": "Hello"
},
{
"languageId": 2,
"name": "Hola"
}
]
}
首先,我创建了一个类别(验证语言的 id 是否真的存储在数据库中),然后使用多对多的 laravel 功能,我将语言和名称附加到类别模型中,如下所示:
foreach($request->names as $name) {
$category->languages()->attach($name['languageId'], ['name' => $name['name']]);
}
现在这似乎工作得很好,并且不用像这样使用 API Resource 过滤语言来检索所有类别是件好事:
public function toArray($request)
{
$category = [];
$category['id'] = $this->id;
$category['languages'] = [];
$category['languages'] = $this->languages->map(function ($language) {
return [
'languageId' => $language->id,
'languageCode' => $language->code,
'languageName' => $language->name,
'categoryName' => $language->pivot->name,
];
});
return $category;
}
这是具有 3 种语言的 3 个类别的输出(伪造数据):
array:3 [
"data" => array:3 [
0 => array:4 [
"id" => 1
"languages" => array:3 [
0 => array:4 [
"languageId" => 1
"languageCode" => "pa"
"languageName" => "Kacey Trantow"
"categoryName" => "quasi"
]
1 => array:4 [
"languageId" => 2
"languageCode" => "ne"
"languageName" => "Mr. Alexandre Heathcote"
"categoryName" => "perferendis"
]
2 => array:4 [
"languageId" => 3
"languageCode" => "kj"
"languageName" => "Mr. Misael Robel"
"categoryName" => "repudiandae"
]
]
"imageUrl" => null
"enabled" => true
]
1 => array:4 [
"id" => 2
"languages" => array:3 [
0 => array:4 [
"languageId" => 1
"languageCode" => "pa"
"languageName" => "Kacey Trantow"
"categoryName" => "non"
]
1 => array:4 [
"languageId" => 2
"languageCode" => "ne"
"languageName" => "Mr. Alexandre Heathcote"
"categoryName" => "vitae"
]
2 => array:4 [
"languageId" => 3
"languageCode" => "kj"
"languageName" => "Mr. Misael Robel"
"categoryName" => "suscipit"
]
]
"imageUrl" => null
"enabled" => true
]
2 => array:4 [
"id" => 3
"languages" => array:3 [
0 => array:4 [
"languageId" => 1
"languageCode" => "pa"
"languageName" => "Kacey Trantow"
"categoryName" => "molestiae"
]
1 => array:4 [
"languageId" => 2
"languageCode" => "ne"
"languageName" => "Mr. Alexandre Heathcote"
"categoryName" => "esse"
]
2 => array:4 [
"languageId" => 3
"languageCode" => "kj"
"languageName" => "Mr. Misael Robel"
"categoryName" => "beatae"
]
]
"imageUrl" => null
"enabled" => true
]
]
现在,问题是当我想过滤仅传递 1 种特定语言的类别时,这是一个经典用例,当用户在导航期间仅使用 1 种语言时,如果我只想要 1 种语言(以及 1 个类别的枢轴关系中的名称)什么我需要什么样的操作?
我已经用特定的过滤器组织了服务,并为任何集合排序,但这种需求似乎让我抓狂!
所以这是我构建过滤器、排序和包含动态的最终 getCategories 方法:
-
成像以便有这样的查询字符串:
http://localhost:8000/api/v1/categories?orderBy=id:asc&include=language&language.code=EN
我想要这样的回应:
array:3 [
"data" => array:3 [
0 => array:4 [
"id" => 1
"languages" => array:3 [
0 => array:4 [
"languageId" => 1
"languageCode" => "EN"
"languageName" => "English"
"categoryName" => "quasi"
]
]
]
1 => array:4 [
"id" => 2
"languages" => array:3 [
0 => array:4 [
"languageId" => 1
"languageCode" => "EN"
"languageName" => "English"
"categoryName" => "non"
]
]
]
2 => array:4 [
"id" => 3
"languages" => array:3 [
0 => array:4 [
"languageId" => 1
"languageCode" => "EN"
"languageName" => "English"
"categoryName" => "molestiae"
]
]
]
]
我需要这样的东西,但一般来说,因为我们有其他实体对多语言系统使用相同的逻辑。
为了澄清我想要的就像这样的连接查询:
SELECT *
FROM categories ca INNER JOIN category_language cl ON ca.id =cl.category_id
INNER JOIN languages lan ON lan.id = cl.language_id
WHERE lan.code = 'EN';
您将仅检索英语语言的类别列表...
感谢您的任何建议和帮助!