如何在Laravel

时间:2016-10-09 10:40:54

标签: laravel put

我正在使用Laravel作为API和Angularjs作为前端来编写Web应用程序。我有一个表单来使用PUT方法更新产品,其中包含一组信息和一个文件作为产品图像。但我无法在控制器中获取输入请求,它是空的。

请参阅以下代码:

web.php(路线)

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

我的angularjs产品服务:

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

            }
        );
    }

我的ProductController更新功能

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

这就是我在Chrome检查员看到的内容 Put header Sended data Result was empty

请分享您在此问题上的经验。感谢。

3 个答案:

答案 0 :(得分:4)

你需要的是只有正常的POST请求,新的字段名为_method = put,那么你的代码将正常工作:

enter image description here

答案 1 :(得分:0)

试试这个方法:

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

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

此外,请确保您使用Route::putRoute::resource作为路线。

答案 2 :(得分:0)

根据this discussion,你无法做到这一点。你应该做的是假装'使用Form Method Spoofing

的PUT请求