【发布时间】:2018-03-21 02:34:33
【问题描述】:
我正在尝试显示存储在数据库中的图像的 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"
}]
}
这是我的控制器中用于从 Post 表中检索存储在数据库中的图像的函数:
public function index()
{
$posts = Post::select(array(DB::raw("CONCAT('http://localhost:8000/images/', image) AS imageurl")))->get();
return response()->json(['posts' => $posts]);
}
任何帮助将不胜感激!
【问题讨论】: