Php Laravel 5.5:向json对象添加url以保存多张图片
我需要用Laravel保存多张图片。图片单独存储在磁盘上,指向这些地方的链接存储在数据库中的JSON文件中。因此,每个用户在数据库中都有一列,其中包含:Php Laravel 5.5:向json对象添加url以保存多张图片,php,json,laravel,Php,Json,Laravel,我需要用Laravel保存多张图片。图片单独存储在磁盘上,指向这些地方的链接存储在数据库中的JSON文件中。因此,每个用户在数据库中都有一列,其中包含: {"images": ["/user/57/house-11-1.png", "/use/57/house-12-2.png"]} 因此,当用户上载文件并单击“保存”时,会出现以下代码: $path = $this->processImage($request, $user->id, $house->id);
{"images": ["/user/57/house-11-1.png", "/use/57/house-12-2.png"]}
因此,当用户上载文件并单击“保存”时,会出现以下代码:
$path = $this->processImage($request, $user->id, $house->id);
/* uploads the image to the server and pass path of the image*/
if ($path) {
$jsonstring = $house->images;
dd($jsonstring);
$arr = json_decode($jsonstring);
$arr['images'] = [$path];
$json = json_encode($arr);
dd($json);
$house->images = $json;
$house->save();
}
使用的laravel模型称为userHouse,并将图像保存为:
class userHouse extends Model implements Changeable
{
protected $casts = [
'images' => 'array',
];
...
}
processImage功能:
private function processImage($request, $userID, $houseId)
{
$path = null;
$number = rand(1, 99);
if ($request->hasFile('image')) {
$image = Image::make($request->file('image'))
->resize(750, null, function ($constraint) {
$constraint->aspectRatio();
})
->encode('png');
$path = "/vendors/{$userID}/horse-{$houseId}-{$number}.png";
Storage::disk('fileadmin')->put($path, $image->encoded);
}
return $path;
}
我得到的错误是:
[2019-05-27 08:01:15] local.ERROR: Serialization of 'Illuminate\Http\UploadedFile' is not allowed {"userId":57,"email":"info@test.com","exception":"[object] (Exception(code: 0): Serialization of 'Illuminate\\Http\\UploadedFile' is not allowed at /Users/dsfsdf/ah-website/user/laravel/framework/src/Illuminate/Session/Store.php:128)
[stacktrace]
如何将url添加到数据库中的JSON字符串中?上载单个图像有效。我能想到的是,
processImage
方法无法处理请求中的多个上载文件
此外,在我看来,这个方法实际上应该接收UploadedFile对象,而不是整个请求
因此,代码类似于:
$images = $house->images;
foreach ($request->images as $uploadedFile) {
$images[] = $this->processImage($uploadedFile, $user->id, $house->id);
}
$house->images = $images;
$house->save();
另外,如果您在模型中将images属性定义为array,那么就不需要对其进行解码/编码,因为laravel已经这样做了。我能想到的是,
processImage
方法无法处理请求中的多个上载文件
此外,在我看来,这个方法实际上应该接收UploadedFile对象,而不是整个请求
因此,代码类似于:
$images = $house->images;
foreach ($request->images as $uploadedFile) {
$images[] = $this->processImage($uploadedFile, $user->id, $house->id);
}
$house->images = $images;
$house->save();
此外,如果您在模型中将images属性定义为array,则无需对其进行解码/编码,因为laravel已经对其进行了解码/编码。似乎
$path
是一个上传文件
对象,而不是服务器上的文件路径。。您可以从此点开始调查$arr['images']=[$path];应该是array_push($arr['images',$path);请确保$path包含正确的路径字符串。似乎$path
是一个上载文件
对象,而不是服务器上的文件路径。。您可以从此点开始调查$arr['images']=[$path];应该是array_push($arr['images',$path);请确保$path包含正确的路径字符串。我一次只保存一个图像。没有多重上传。我在原始问题中添加了processImage的代码。我认为错误来自其他地方,除非您将存储设置为会话。我在您的帮助下更新了我的代码。现在没有错误,但它不会将图像添加到json数据库列。我是否应该通过$image=image::make($uploadedFile)在processImage中加载图像?我一次只保存一个图像。没有多重上传。我在原始问题中添加了processImage的代码。我认为错误来自其他地方,除非您将存储设置为会话。我在您的帮助下更新了我的代码。现在没有错误,但它不会将图像添加到json数据库列。我是否应该通过$image=image::make($uploadedFile)在processImage中加载图像?