Fetching documents from Vue within Laravel

I managed to successfully upload files from my frontend using Vue to Laravel backend.

The upload functionality was implemented using this code snippet:

addPost() {
axios.post('api/submitPropertyPhoto?token=' + this.token, this.postFormData)
 .then(r => {
 console.log(r)
})
.catch(e => {
console.log(e)
 }
},
uploadFieldChange(e) {
  for(let key in e.target.files) {
  this.postFormData.append('images[]', e.target.files[key])
   }
 }

However, when I attempted to retrieve the file using the normal Laravel request file helper method, it returned nothing. Strangely, when I used dd($request->files), it provided the following details:

FileBag {#68
  #parameters: array:1 [
  "images" => array:1 [
  0 => array:1 [
    "name" => UploadedFile {#45
      -test: false
      -originalName: "error.JPG"
      -mimeType: "image/jpeg"
      -size: 21806
      -error: 0
      path: "C:\xampp\tmp"
      filename: "php639F.tmp"
      basename: "php639F.tmp"
      pathname: "C:\xampp\tmp\php639F.tmp"
      extension: "tmp"
      realPath: "C:\xampp\tmp\php639F.tmp"
      aTime: 2018-05-17 04:26:29
      mTime: 2018-05-17 04:26:29
      cTime: 2018-05-17 04:26:29
      inode: 0
      size: 21806
      perms: 0100666
      owner: 0
      group: 0
      type: "file"
      writable: true
      readable: true
      executable: false
      file: true
      dir: false
      link: false
      linkTarget: "C:\xampp\tmp\php639F.tmp"
     }
    ]
  ]
 ]
}

My main objective is to store the uploaded file on the server disk and save its filename in the database.

Answer №1

To update your controller, consider implementing the following code:

$urls = collect();
foreach ($request->images as $image) {
    $path = $image->store('images');
    // Add $path to your database
    $urls->merge(Storage::url($path));
}
return response($urls);

Next, in Vue.js, you can fetch the URLs like so:

addPost() {
    axios.post('api/submitPropertyPhoto?token=' + this.token,this.postFormData)
       .then(r => {
           console.log(r.data); // You should receive a JSON object containing the URLs.
       })
       .catch(e => {
           console.log(e)
       }

},

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

One way to specify a callback is by embedding a function within another function. This can be accomplished in JavaScript ES

A.file function Carousel(){ ~~~~~~ code ~~~~~~ this.add = function(data){ do_something() self.addCallback.call(this, arguments) }; this.remove = function(data){ do_something() this.removeCallback.call(this, arguments) }; ...

Is it possible to send both a page and personalized information to the browser at the same time?

Is there a way to efficiently render a page and transmit custom data to the browser at the same time? It seems like I need to send two layers: one with the template and another with JSON data. My goal is to manage this data using Backbone. From the tutori ...

PHP script for uploading files and moving them to a specific folder

This code snippet showcases a simple PHP script for file upload: <?php if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"] ...

Is it possible to use Ajax to prompt a pop-up window for basic authentication when logging in?

While attempting to access the reed.co.uk REST web API in order to retrieve all related jobs, I am encountering an issue. Despite passing my username and password, a popup window keeps appearing when I call the URL. The alert message displayed reads: i ...

Looking to trigger the closing of a q-popup-proxy by clicking a button from a separate component

I am currently working with a q-popup-proxy component in my project. <q-btn label="Add Fault" class="addFaultButton" dense @click="openPopUp()"> <q-popup-proxy position="center" v-if="openFaults" ...

Learn how to stop the automatic scroll-to-top behavior on specific links within your React-Router application by implementing a custom ScrollToTop component

While utilizing React-Router and a custom ScrollToTop component to reset the scroll position on navigation, I encountered a specific scenario where I required the scroll position to remain unchanged when clicking on a category item. Custom ScrollToTop Com ...

Simultaneous appearance of multiple jQuery dialogs

As I work on creating a team page, each team member has their own div that contains a photo and employee information. My goal is to have a dialog box pop up when the photo is clicked, using the $this context to retrieve the employee data from the same div. ...

Utilizing updateProfile with Firebase and Angular: A comprehensive guide

I'm facing an issue while trying to update a user's displayName and/or photo URL in Firebase using Angular. I have a service set up to handle authentication tasks, which is working fine for login/register functions. Additionally, I've create ...

Creating interactive JavaScript elements that can be moved around within a container

I recently faced a challenge while attempting to make draggable elements within a div. The issue arose when I realized that I couldn't figure out how to drag each element individually without affecting the others. My current code only allows for handl ...

HTML Scripts: Enhancing Web Functionality

I have another question regarding executing a script (docs()) upon clicking on text. I attempted to use the following code: <p><span class="skill">Documentation can be found here.<script>docs()</script></span></p> Unfo ...

Modifying the input placeholder color with ng-style

I am working on setting the color of my input placeholder using a dynamic color defined in a $scope variable in my controller for my HTML code. The $scope variable is structured as follows: $scope.primaryColor={"color":colorVar}; //colorVar represents th ...

In what ways could you tackle an image puzzle web application, would it be on the server side or the client side?

I am currently in the process of developing a browser-based game that involves taking an image file as input, dividing it into blocks, and randomly rearranging the blocks for users to solve and restore the original picture. For this project, I have decide ...

What is the most effective method for sharing features while maintaining unique aesthetics?

In the process of developing a Next.js website, I've encountered a challenge: I have 3 forms that function in similar ways but have different styles. Instead of duplicating code for each form, I'm looking for a way to build the form structure on ...

Is there a way to generate a button that displays the subsequent 5 outcomes from the database without navigating away from the current

I am looking to implement a button on my webpage that, once clicked, will load the next set of five questions from my database. I prefer not to use pagination or the GET method to prevent users from navigating back to previously answered questions. My goa ...

Currently experimenting with jQuery in conjunction with the reddit API to generate individual divs for every post

Issue with displaying post titles, URLs, and permalinks separately. Need them to display together for each post. See the code below: $(document).ready(function() { $.getJSON( "http://www.reddit.com/r/pics.json?jsonp=?", function foo(data) { ...

Adding data to the SharePoint RichText Control using the WinForms WebBrowser Control

I am struggling to modify the content of an SP Rich Text field using the WinForms web browser control. While changing the values of other controls is straightforward, I have encountered difficulties with Rich Text fields. I found some suggestions on this b ...

Passing parameters from an Objective-C controller class to the index.html file in a PhoneGap application

What is the best way to pass parameters from an Objective-C controller class to index.html in a PhoneGap app using JavaScript? ...

How can I create a jumping animation effect for my <li> element in JQuery?

I have a horizontal navigation bar with a "JOIN" option. My goal is to make the #jump item continuously jump up and down after the page has finished loading. Currently, I have this code which only triggers the jump effect once when hovering over the eleme ...

What is the best way to display a div based on a keyword match?

If the keyword search results in a match, I can display the corresponding input text and its related category div. Now, I am attempting to also search through category names. If the searched keyword matches a category name, that specific div should be visi ...

A guide on utilizing the intl.formatRelativeTime() function effectively

I need to display messages like "created 1 hour ago" or "created 1 day ago," as well as in plural form, such as "created 10 minutes ago" or "created 3 days ago." To achieve this, I am attempting to utilize the FormatJS API, specifically the intl.formatRela ...