gpt4 book ai didi

php - Laravel - 有什么方法可以在不使用 sql 连接的情况下检索 json 格式的图像文件名及其完整 URL

转载 作者:行者123 更新时间:2023-12-02 03:38:32 24 4
gpt4 key购买 nike

我在 Laravel 中编写了一些代码,以仅显示 mysql 数据库中 Post 表中的图像。

这是显示图像的函数

public function index()
{
$posts = Post::all()->pluck('image');
return response()->json(['images' => $posts]);
}

这是我收到的响应,它在 JSON 数组中显示图像文件名

{
"images": [
"1509695371.jpg",
"1509696465.jpg",
"1509697249.jpg"
]
}

但我想用完整的 URL 显示它们,就像下面的 json 格式一样。在该函数中使用 Laravel eloquent 但不使用 sql 连接会更好。

{
"images": [
"http://localhost:8000/images/1509695371.jpg",
"http://localhost:8000/images/1509696465.jpg",
"http://localhost:8000/images/1509697249.jpg"
]
}

任何帮助将更加感激!

最佳答案

您可以使用map您的收藏方法:

public function index()
{
$posts = Post::all()->pluck('image')->map(function($image){
return "http://localhost:8000/images/".$image;
});
return response()->json(['images' => $posts]);
}

关于php - Laravel - 有什么方法可以在不使用 sql 连接的情况下检索 json 格式的图像文件名及其完整 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49391723/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com