php - laravel - 如何连接 URL 并以 json 格式从数据库检索图像

标签 php mysql laravel

我正在尝试显示存储在数据库中的图像的 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/

相关文章:

php - magento 自定义支付方式重定向

php - 在 codeigniter 中获取表格的总列字段?

php - Symfony 中预期的不同类型

php - 在 Laravel 中缓存每个数据库查询是否健康?

laravel - Group_concat - 查询生成器

php - 使用 PHP 执行 MPI 命令

php - 如何根据另一个字符串的值获取表值?

php - 使用 PHP/MySQL 搜索网站的最佳方式?

php - Laravel str_slug 不适用于 unicode bangla

php - 使用 PHP 的人类可读日期