Mehdi Yaghoubi
Mehdi Yaghoubi

Reputation: 653

How to Store Images uploaded from React Native in Database and Image Folder by Laravel Backend

I get a response from Laravel the image or Images I have uploaded from React native, Here is the response code. How I can save name in database and image/images file in the folder?

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

Upvotes: 1

Views: 1244

Answers (1)

elegasoft
elegasoft

Reputation: 66

You didn't include any controller code to show what you have already, but here is something to get you started.

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

Upvotes: 1

Related Questions