georgeos's avatar

Upload file to Laravel as API

Hello all, I'm experiencing a problem with my Laravel API, actually, I'm not sure if the problem is in my Laravel API or my frontend App (if so, sorry for bothering in this forum). The situation is this.

I'm trying to upload a single file from the frontend into the laravel API. For this, I'm using AngularJS and ng-file-upload. From postman, I'm using these settings (http://i.stack.imgur.com/o9LHM.jpg) :

  • Method: Post
  • URL: api/account/1?&_method=PUT
  • Params:
    • _method: PUT
  • Body:
    • name: Test
    • logo: A selected file from my computer

Using these settings, Postman is uploading the file successfully. In my Laravel controller, I have this:

public function update(Account $account)
    {
        $name = Input::get('name');
        $logo = Input::file('logo');
        $account->name = $name;
        $account->logo = $logo;
        $account->save();
        return response()->json(["message"=>"updated"]);
    }

But from my AngularJS, it appears like I'm using the same approach but my Laravel API looks like is not receiving nothing! This is my relevant code about the Laravel API call on AngularJS.

Upload.upload({
        url: 'api/account/'+ $scope.id,
        method: 'PUT',
        file: logo,
        sendFieldsAs: 'form-data',
        fields: { name: $scope.name },
    })

If anyone has a clue, please let me know. I think the problem is with the PUT method, but I'm not sure. I have been dealing with this problem by some hours ago.

BTW, I already used another way to upload a file from Angular, and the result is the same. That's the reason that I decided to open this thread, because probably is the way I'm obtaining the data from my Laravel controller. Thanks in advance.

0 likes
3 replies
michaeldyrynda's avatar

Try setting the URL to either '/api/account/' + $scope.id or the full URL (including protocol and domain).

Posting to api/account/' + $scope.id without the leading slash will likely post to a route relative to the current URL.

Your browser's developer tools should show the network request to confirm this, though. Make sure the request is being sent.

georgeos's avatar

Thanks @deringer but it didn't work.

The request is being sent. Even if I do a var_dump('hello worl'); from my controller, I can see it in the response from the browser's developers tools.

Everything is fine, but it seems that when Laravel search for the Input's they are null.

Any other idea?

Please or to participate in this conversation.