php dd($request->all()); 返回空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30334216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:43:24  来源:igfitidea点击:

dd($request->all()); gives back empty array

phplaravel

提问by chloealee

I am trying to upload a photo from my Laravel 5 app to be stored in AWS. I am using the Postman REST client to test. When I upload a photo, the request returns an empty array. Does anyone know why this might be? Here's the code for my Avatar Controller:

我正在尝试从我的 Laravel 5 应用程序上传照片以存储在 AWS 中。我正在使用 Postman REST 客户端进行测试。当我上传照片时,请求返回一个空数组。有谁知道为什么会这样?这是我的头像控制器的代码:

class AvatarController extends Controller
{

  public function __construct(AWS $aws)
  {
      $this->aws = $aws;
  }

/**
 * Store a new avatar for a user.
 * POST northstar.com/users/{id}/avatar
 */
  public function store(User $user, Request $request)
  {
    dd($request->all());
    // dd($request->file('photo'));

    $file = $request->file('photo');
    // $file = Request::file('photo');
    // $file = Input::file('photo');

    $v = Validator::make(
      $request->all(),
      ['photo' => 'required|image|mimes:jpeg,jpg|max:8000']
    );

    if($v->fails())
      return Response::json(['error' => $v->errors()]);         

    $filename = $this->aws->storeImage('avatars', $file);

    // Save filename to User model
    $user->avatar = $filename;
    $user->save();

    // Respond to user with success
    return response()->json('Photo uploaded!', 200);
  }
}

回答by chloealee

Found the answer - looks like there was an issue with my headers in Postman. I had both Accept application/json and Content-Type application/json. Once I removed Content-Type, all is fixed. Thanks!

找到了答案 - 看起来我在 Postman 中的标题有问题。我有 Accept application/json 和 Content-Type application/json。一旦我删除了 Content-Type,一切就都解决了。谢谢!

回答by user3601546

Bit late to the party, but might be usefull for others: My problem was that the Content-Typeheader value was application/jsonwhile the actual payload was form data. Changing the header to application/x-www-form-urlencodedfixed the issue.

参加聚会有点晚,但可能对其他人有用:我的问题是Content-Type标题值是application/json实际有效负载是表单数据。更改标题以application/x-www-form-urlencoded解决问题。

回答by stillatmylinux

You might want to test that the JSON is valid

您可能想要测试 JSON 是否有效

$data = $request->all();
if(empty($data)) {
    $data = json_decode($request->getContent());
    $data = json_decode($data);

    if(is_null($data)) {
        return response()->json("Not valid json", 400);
    }
}

回答by HoseinGhanbari

The same issue on postman, while using PUT/PATCH method directly. One possible solution is to send your PUT/PATCH request as a POST, but include proper _methodin the request body along with other parameters.

邮递员也有同样的问题,直接使用 PUT/PATCH 方法。一种可能的解决方案是将您的 PUT/PATCH 请求作为 POST 发送,但在请求正文中包含适当的_method以及其他参数。

_method: PUT

_method: PUT

Hope this helps

希望这可以帮助

回答by megaman

Just want to add where i made a mistake. When sending POST request with Postman, you also have to make sure the json is correct.

只是想补充一下我犯错的地方。使用 Postman 发送 POST 请求时,您还必须确保 json 是正确的。

// invalid json (notice the ending comma)
{
    "akey":"aval",
    "nkey": "bval",
}

//valid json (no ending comma)
{
    "akey":"aval",
    "nkey": "bval"
}

回答by Asim Rafique

simply change in your Rout file

只需更改您的 Rout 文件

 Route::get('/users', 'AvatarController@store');

to

Route::POST('/users', 'AvatarController@store');

回答by Imtiaz Pabel

try use this

尝试使用这个

dd($request->all());

回答by Pyton

Try dd($request), dd($_REQUEST), dd($request->files).

尝试 dd($request), dd($_REQUEST), dd($request->files)。

回答by David Nguyen

Should be using:

应该使用:

Request::all();

Request::all();

As per documents: http://laravel.com/docs/5.0/requests

根据文件:http: //laravel.com/docs/5.0/requests