如何在将对象值发送到 json 编码之前将 url 的第一部分添加到对象值中
How to add the first part of an url in a object value before sending it to json encode
我在数据库中查询图像列表:
$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 值,然后在将其发送到 [=26= 之前将值重写回对象]编码。
我尝试了以下方法,但也许我对问题的处理方法不对。
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;
});
我在数据库中查询图像列表:
$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 值,然后在将其发送到 [=26= 之前将值重写回对象]编码。
我尝试了以下方法,但也许我对问题的处理方法不对。
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;
});