我正在尝试显示存储在数据库中的图像的 json 数组,以在 Laravel 中提供完整的 URL。我正在使用 CONCAT() 函数来连接图像的完整 URL,但我得到了一个错误的 URL,其中包含许多破折号。 这是即将输出的问题:
{
"posts": [{
"imageurl": "http:\/\/localhost:8000\/images\/1509695371.jpg"
}, {
"imageurl": "http:\/\/localhost:8000\/images\/1509695156.jpg"
}, {
"imageurl": "http:\/\/localhost:8000\/images\/1509696465.jpg"
}, {
"imageurl": "http:\/\/localhost:8000\/images\/1509697249.jpg"
}]
}
这是我的 Controller 中从 Post 表检索存储在数据库中的图像的函数:
public function index()
{
$posts = Post::select(array(DB::raw("CONCAT('http://localhost:8000/images/', image) AS imageurl")))->get();
return response()->json(['posts' => $posts]);
}
任何帮助将不胜感激!
最佳答案
非常感谢@Sari 现在工作正常。将代码更改为这样后,
public function index()
{
$posts = json_encode(Post::select(array(DB::raw("CONCAT('http://localhost:8000/images/', image) AS imageurl")))->get(), JSON_UNESCAPED_SLASHES);
return $posts;
}
关于php - laravel - 如何连接 URL 并以 json 格式从数据库检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49389694/