我查询数据库以获取图像列表:
$rows = $db->table('images')->where('attivo', 1)->get();
我得到以下对象:
object(Illuminate\Support\Collection)#107 (1) {
["items":protected]=>
array(5) {
[0]=>
object(stdClass)#96 (12) {
["id"]=>
int(3)
["img"]=>
string(11) "example.jpg"
["created_at"]=>
string(19) "2018-07-26 20:33:59"
["attivo"]=>
int(1)
}
[1]=>
object(stdClass)#88 (12) {
["id"]=>
int(4)
["img"]=>
string(12) "img.png"
["created_at"]=>
string(19) "2018-07-26 20:33:59"
["attivo"]=>
int(1)
}
[2]=>
object(stdClass)#103 (12) {
["id"]=>
int(5)
["img"]=>
string(9) "test2.jpg"
["created_at"]=>
string(19) "2018-07-27 13:42:13"
["attivo"]=>
int(1)
}
}
}
我需要通过将url的头部(http://www.example.com/folder/)放在它之前来修改 img 值,然后在将它发送到 json 编码之前将值重写回对象。
我尝试了以下操作,但也许我对问题的处理方法错误。
foreach ($rows as $k => $value) {
if($k == 'img'){
echo $rows['img'] = "http://example.com/folder/".$value->img;
}
}
你能给我一个方向吗?
最后我找到了解决方案。我不得不使用map
:
$rows->map(function ($value) {
$value->img = 'http://example.com/img/'. $value->img;
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句