【问题标题】:How to Store Images uploaded from React Native in Database and Image Folder by Laravel Backend如何通过 Laravel 后端将 React Native 上传的图像存储在数据库和图像文件夹中
【发布时间】:2019-12-31 16:09:46
【问题描述】:

我从 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

【问题讨论】:

  • 您应该展示您的控制器代码或您迄今为止尝试过的内容,以便人们可以帮助您。

标签: laravel react-native


【解决方案1】:

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

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);
}

【讨论】:

  • 您应该将该评论作为答案,以便其格式正确,然后接受您自己的答案。
猜你喜欢
  • 2021-07-30
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2014-12-11
  • 2020-03-06
  • 1970-01-01
  • 2010-09-22
  • 2018-03-28
相关资源
最近更新 更多