這是來自 Laravel DB 的資料串列示例:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[category] => Protections menstruelles
[sub_category] => Sous-vêtements lavables
[model] => Culotte menstruelle
)
[1] => stdClass Object
(
[category] => T-shirt, Top & Polo
[sub_category] => Débardeur, Caraco
[model] => Bustier
)
[2] => stdClass Object
(
[category] => Protections menstruelles
[sub_category] => Sous-vêtements lavables
[model] => Tanga menstruel
)
[3] => stdClass Object
(
[category] => Protections menstruelles
[sub_category] => Sous-vêtements lavables
[model] => Body menstruel
)
[4] => stdClass Object
(
[category] => Protections menstruelles
[sub_category] => Maillot de bain menstruel
[model] => 1 pièce
)
[5] => stdClass Object
(
[category] => Homewear
[sub_category] => Chaussettes
[model] => Socquettes
)
[6] => stdClass Object
(
[category] => Plage
[sub_category] => Bas de maillot
[model] => Short de bain
)
[7] => stdClass Object
(
[category] => Lingerie & Sous-vêtement
[sub_category] => Soutien-gorge
[model] => Triangle
)
[8] => stdClass Object
(
[category] => Lingerie & Sous-vêtement
[sub_category] => Culotte, tanga & string
[model] => Tanga
)
[9] => stdClass Object
(
[category] => Lingerie & Sous-vêtement
[sub_category] => Culotte, tanga & string
[model] => Culotte
)
)
)
我想創建一個 JSON 物件,它可能是這樣的:
var subjectObject = {
"Protections menstruelles": {
"Maillot de bain menstruel": ["1 pièce"],
"Sous-vêtements lavables": ["Body menstruel"]
},
"Lingerie & Sous-vêtement": {
"Soutien-gorge": ["Triangle"],
"Culotte, tanga & string": ["Tanga", "Culotte"]
}
...
}
這個想法是在 select 中提出一些問題,并使用 JavaScript 有條件地根據先前的 select 選擇選項。我想為此使用本教程:https ://www.w3schools.com/howto/howto_js_cascading_dropdown.asp 我需要幫助來構建 json 輸入。
它正在處理這個 laravel DB 請求:
->select('category','sub_category','model')
->whereNotNull('category')
->whereNotNull('sub_category')
->whereNotNull('model')
->distinct()
->get()
->groupBy('category')
->map(function($item){
return collect($item)
->groupBy('sub_category')
->map(function($item){
return collect($item)->pluck('model');
})->toArray();
})->toJson();
但我不能在 javascript 中使用它:
var subjectObject = "{{ $collection }}";
它不是示例中的 json:
var subjectObject = {
"Front-end": {
"HTML": ["Links", "Images", "Tables", "Lists"],
"CSS": ["Borders", "Margins", "Backgrounds", "Float"],
"JavaScript": ["Variables", "Operators", "Functions", "Conditions"]
},
"Back-end": {
"PHP": ["Variables", "Strings", "Arrays"],
"SQL": ["SELECT", "UPDATE", "DELETE"]
}
}
uj5u.com熱心網友回復:
這是一個集合。您可以使用這些方法:
groupBy方法按給定鍵對集合的專案進行分組map方法遍歷集合并將每個值傳遞給給定的回呼pluck方法檢索給定鍵的所有值,并且toJson方法將集合轉換為 JSON 序列化字串
$collection
->groupBy('category')
->map(function($item){
return collect($item)
->groupBy('sub_category')
->map(function($item){
return collect($item)->pluck('model');
})
->toArray();
})
->toJson();
輸出 :
{
"Protections menstruelles": {
"Sous-vêtements lavables": [
"Culotte menstruelle",
"Tanga menstruel",
"Body menstruel"
],
"Maillot de bain menstruel": [
"1 pièce"
]
},
"T-shirt, Top & Polo": {
"Débardeur, Caraco": [
"Bustier"
]
},
"Homewear": {
"Chaussettes": [
"Socquettes"
]
},
"Plage": {
"Bas de maillot": [
"Short de bain"
]
},
"Lingerie & Sous-vêtement": {
"Soutien-gorge": [
"Triangle"
],
"Culotte, tanga & string": [
"Tanga",
"Culotte"
]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/425342.html
