首页 > 解决方案 > 如何通过 Laravel 后端将 React Native 上传的图像存储在数据库和图像文件夹中

问题描述

我从 Laravel 收到从 React native 上传的图像或图像的响应,这是响应代码。如何将名称保存在文件夹中的数据库和图像/图像文件中?

images: Array(6)
0: {uri: "file:///data/user/0/com.pardis/cache/react-native-image-crop-picker/IMG_20191226_174428.jpg", width: 52.747252747252745, height: 70, mime: "image/jpeg"}
1: {uri: "file:///data/user/0/com.pardis/cache/react-native-image-crop-picker/IMG_20191226_174441.jpg", width: 52.747252747252745, height: 70, mime: "image/jpeg"}
2: {uri: "file:///data/user/0/com.pardis/cache/react-native-image-crop-picker/IMG_20191226_174453.jpg", width: 52.747252747252745, height: 70, mime: "image/jpeg"}
3: {uri: "file:///data/user/0/com.pardis/cache/react-native-…p-picker/4d6f5a93-ff0c-4987-a014-d17cfbcb5a22.jpg", width: 52.747252747252745, height: 70, mime: "image/jpeg"}
4: {uri: "file:///data/user/0/com.pardis/cache/react-native-image-crop-picker/IMG_20191226_174437.jpg", width: 52.747252747252745, height: 70, mime: "image/jpeg"}
5: {uri: "file:///data/user/0/com.pardis/cache/react-native-…p-picker/e49a5c1c-b03d-4cee-b69b-16c9ee82af5b.jpg", width: 52.747252747252745, height: 70, mime: "image/jpeg"}
length: 6

标签: laravelreact-native

解决方案


您没有包含任何控制器代码来显示您已经拥有的内容,但这里有一些东西可以帮助您入门。

public function store(Request $request)
{
    //First, get all of the images with temp paths as a collection
    $images = collect($request->files('images'));
    // Next, persist the images in local storage and get their file paths
    $filenames = $images->map(function($image){
      return ['path' => $image->store('public')];  
      // Where public is the name of the storage disk you have defined in config/filesystems.php
    });
    // Finally, bulk insert the image paths in the database
   Image::insert($filenames);
}

推荐阅读