Laravel 将图像路径附加到对象数组
Laravel append image path to array of object
用于制作 api 到 return 医生的所有图像
$images = Doctor::select('image')->get();
所以响应就像
`[
{ image: 111.png },
{ image: 222.png },
{ image: 333.png }
]`
然后我 return 图片
return $images
我想在 return 之前附加图像的路径,就像
mypath/222.png
我可以用 for 循环来做到这一点,谢谢,这增加了不必要的复杂性
他们在 eloquent
中取得成功的方法也是如此
如果这就是您希望每次获取图像列的方式,那么访问器是最佳选择。在您的 Doctor
模型中:
public function getImageAttribute($value){
return 'mypath/'.$value;
}
如果你只想在这个地方使用它,那么你可以使用 map
:
$images = Doctor::select('image')->get()->map(function($doctor){
$doctor->image = 'mypath/'.$doctor->image;
return $doctor;
});
你会得到你想要的结果。
用于制作 api 到 return 医生的所有图像
$images = Doctor::select('image')->get();
所以响应就像
`[
{ image: 111.png },
{ image: 222.png },
{ image: 333.png }
]`
然后我 return 图片
return $images
我想在 return 之前附加图像的路径,就像
mypath/222.png
我可以用 for 循环来做到这一点,谢谢,这增加了不必要的复杂性
他们在 eloquent
中取得成功的方法也是如此如果这就是您希望每次获取图像列的方式,那么访问器是最佳选择。在您的 Doctor
模型中:
public function getImageAttribute($value){
return 'mypath/'.$value;
}
如果你只想在这个地方使用它,那么你可以使用 map
:
$images = Doctor::select('image')->get()->map(function($doctor){
$doctor->image = 'mypath/'.$doctor->image;
return $doctor;
});
你会得到你想要的结果。