12

I am programing a web app using Laravel as API and Angularjs as frontend. I have a form to update product using PUT method with a array of informations and a file as product image. But I couldn't get the input requests in the controller, it was empty.

Please see the code below :

web.php ( route )

Route::group(['prefix' => 'api'], function()
{
    Route::put('products/{id}', 'ProductController@update');
});

My angularjs product service :

function update(productId, data, onSuccess, onError){
        var formData = new FormData();
        formData.append('imageFile', data.imageFile);
        formData.append('image', data.image);
        formData.append('name', data.name);
        formData.append('category_id', data.category_id);
        formData.append('price', data.price);
        formData.append('discount', data.discount);
        Restangular.one("/products", productId).withHttpConfig({transformRequest: angular.identity}).customPUT(formData, undefined, undefined,   {'Content-Type': undefined}).then(function(response) {

                onSuccess(response);

            }, function(response){

                onError(response);

            }
        );
    }

My ProductController update function

public function update(Request $request, $id) {
// Just print the request data
        dd($request->all());
    }

This is what I see in Chrome inspectmen Put headerSended data Result was empty

Please share your experiences on this problem. Thanks.

2
  • 1
    Paste your routes, views and controllers code snippet please. Let us know what you have tried so far. Without these information no one can help with this little information. Commented Oct 9, 2016 at 10:43
  • I updated the question. Please have a look. Thank you. Commented Oct 10, 2016 at 9:15

3 Answers 3

13

what you need is Only normal POST request with new field named _method=put then your code will work normally:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Laravel fake PUT by using POST but with hidden input text named _method
3

You can't do that, according to this discussion. What you should do instead is to 'fake' the PUT request by using Form Method Spoofing

Comments

1

Try this method:

public update(Request $request, $id)
{
    $request->someVar;
    $request->file('someFile');

    // Get variables into an array.
    $array = $request->all();

Also, make sure you're using Route::put or Route::resource for your route.

1 Comment

$request->all() was empty. Please help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.