У меня есть таблица, зовут шаблоны. В котором присутствует поле id и image_name. в качестве значения image_name используется image1.png, image2.png. Теперь я должен отправить image_name с указанием пути к папке в JSON почтальону. Я пытался с этим. Но не думайте, что это не сработает.
Мой контроллер:
public function TemplateList()
{
$items = array();
$templates = TemplateList::all();
foreach($templates as $key=>$value)
{
$items['key'][] = $key;
$items['value'][] = 'www.xyz.com/images/'.$value;
}
if ($templates->count()) {
$data = collect(["status" => ["code" => "100", "message" => "Success", "data" => $items]]);
} else {
$data = collect(["status" => [ "code" => "101", "message" => "No Records"]]);
}
return response()->json($data, 200);
}
Если я не использую foreach l oop, то в результате получается:
{
"status": {
"code": "100",
"message": "Success",
"data": [
{
"id": 66,
"tempimage": "template1.png"
},
{
"id": 67,
"tempimage": "template2.png"
},
Но когда я использую foreach l oop, результат будет:
{
"status": {
"code": "100",
"message": "Success",
"data": {
"key": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29
],
"value": [
"www.xyz.com/images/{\"id\":66,\"tempimage\":\"template1.png\"}",
"www.xyz.com/images/{\"id\":67,\"tempimage\":\"template2.png\"}",
"www.xyz.com/images/{\"id\":68,\"tempimage\":\"template3.png\"}",
"www.xyz.com/images/{\"id\":69,\"tempimage\":\"template4.png\"}",
"www.xyz.com/images/{\"id\":70,\"tempimage\":\"template5.png\"}",
"www.xyz.com/images/{\"id\":71,\"tempimage\":\"template6.png\"}",
"www.xyz.com/images/{\"id\":72,\"tempimage\":\"template7.png\"}",
"www.xyz.com/images/{\"id\":73,\"tempimage\":\"template8.png\"}",
"www.xyz.com/images/{\"id\":74,\"tempimage\":\"template9.png\"}",
"www.xyz.com/images/{\"id\":75,\"tempimage\":\"template10.png\"}",
"www.xyz.com/images/{\"id\":76,\"tempimage\":\"template11.png\"}",
"www.xyz.com/images/{\"id\":77,\"tempimage\":\"template12.png\"}",
"www.xyz.com/images/{\"id\":78,\"tempimage\":\"template13.png\"}",
"www.xyz.com/images/{\"id\":79,\"tempimage\":\"template14.png\"}",
"www.xyz.com/images/{\"id\":80,\"tempimage\":\"template15.png\"}",
"www.xyz.com/images/{\"id\":81,\"tempimage\":\"template16.png\"}",
"www.xyz.com/images/{\"id\":82,\"tempimage\":\"template17.png\"}",
"www.xyz.com/images/{\"id\":83,\"tempimage\":\"template18.png\"}",
"www.xyz.com/images/{\"id\":84,\"tempimage\":\"template19.png\"}",
"www.xyz.com/images/{\"id\":85,\"tempimage\":\"template20.png\"}",
"www.xyz.com/images/{\"id\":86,\"tempimage\":\"template21.png\"}",
"www.xyz.com/images/{\"id\":87,\"tempimage\":\"template22.png\"}",
"www.xyz.com/images/{\"id\":88,\"tempimage\":\"template23.png\"}",
"www.xyz.com/images/{\"id\":89,\"tempimage\":\"template24.png\"}",
"www.xyz.com/images/{\"id\":90,\"tempimage\":\"template25.png\"}",
"www.xyz.com/images/{\"id\":91,\"tempimage\":\"template26.png\"}",
"www.xyz.com/images/{\"id\":92,\"tempimage\":\"template27.png\"}",
"www.xyz.com/images/{\"id\":93,\"tempimage\":\"template28.png\"}",
"www.xyz.com/images/{\"id\":94,\"tempimage\":\"template29.png\"}",
"www.xyz.com/images/{\"id\":95,\"tempimage\":\"template30.png\"}"
]
}
}
}