0

I'm passing the following request to laravel api, but when I dump the input request, Laravel is returning an empty array.

{
  "expense_type" : "payment",
  "description" : "test",
  "notes" : "My notes",
  "expense_date": "2019-01-15",
  "cost" : 100.50,
  "group_id" : 1,
  "shares" : [
    {
        "user_id" : 1,
        "paid_share" : 100.50,
        "owed_share" : 00.00
    },
    {
        "user_id" : 2,
        "paid_share" : 00.00,
        "owed_share" : 50.25
    },
    {
        "user_id" : 3,
        "paid_share" : 00.00,
        "owed_share" : 50.25
    }
  ]
 }

The output when dumping the input $request->all() is []

can some one help if there is something wrong in the request

Reiah Paul Sam
  • 555
  • 6
  • 16

1 Answers1

0

When you send data in request as json data then $request->all() will always return empty. In order to access data in $request->all() variable you have to send form data not json data.

So, when json data is sent through api, you can access it as array in the controller like this:

$data = json_decode(file_get_contents("php://input"), true);

Now, you can access the values like $data['expense_type']

I hope you understand.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84