Tuesday, April 11, 2017

Laravel get the sent data to the request

I am implementing an API using Laravel 5.4 . I want to send the title, description, time and user_id as a JSON and after that to get the JSON response with the input data.

Here is my code:

    $title = $request->input('title');
    $description = $request->input('description');
    $time = $request->input('time');
    $user_id = $request->input('user_id');

    $meeting = [
        'title' => $title,
        'description' => $description,
        'time' => $time,
        'user_id' => $user_id,
        'view_meeting' => [
            'href' => 'api/v1/meeting/1',
            'method' => 'GET1'
        ]
    ];

    $response = [
      'msg' => 'Meeting created',
      'meeting' => $meeting
    ];

    return response()->json($response, 201);

After running the server, I make a post request using POSTMAN (body->raw:)

{
"time": "201601301330CET",
"title": "Test meeting 2",
"description": "Test",
"user_id": 2
}

But it return this:

{
"msg": "Meeting created",
"meeting": {
  "title": null,
  "description": null,
  "time": null,
  "user_id": null,
  "view_meeting": {
    "href": "api/v1/meeting/1",
    "method": "GET1"
  }
}
}

Why the title, description, time and user_id fields are null?



via xz ayt

Advertisement