Laravel - retrieve data inside controller – POST x PATCH

Viewed 47

First important information: I’m new to laravel, so your patience is appreciated.

I’m currently migrating a framework of mine to laravel, so I’m in the early stages.

Currently, I’m trying to set up an API endpoint to make small changes on some records. I’ve already managed to set up a API for inserting records and works perfectly. However, for setting up an API for small changes (patch), I’m having difficulties, probably because I’m not fully familiar with laravel’s Request class.

My successful insert set up looks like this:

\routes\api.php

Route::post('/categories/',[ApiCategoriesInsertController::class, 'insertCategories'], function($insertCategoriesResults) {
    return response()->json($insertCategoriesResults);
})->name('api.categories.insert');

\app\Http\Controllers\ApiCategoriesInsertController.php

// some code 
public function insertCategories(Request $req): array
{
    $this->arrCategoriesInsertParameters['_tblCategoriesIdParent'] = $req->post('id_parent');

    // some code
}

With this set up, I’m able to retrieve “id_parent” data set through POST.

So, I tried to do exactly the same architecture for patch, but doesn’t seem to work:

\routes\api.php

Route::patch('/records/',[ApiRecordsPatchController::class, 'patchRecords'], function($patchRecordsResults) {
    return response()->json($patchRecordsResults);
})->name('api.records.patch');

\app\Http\Controllers\ApiRecordsPatchController.php

// some code
public function patchRecords(Request $req): array
{
    $this->arrRecordsPatchParameters['_strTable'] = $req->post('strTable');

    // some code
}

In this case, I´m using postman (PATCH request), testing the data in the "Body tab" with key "strTable" and value "123xxx" and I´m receiving “strTable” as null.

Any idea of why this is happening or if I should use another method in the Request class?

Thanks!

2 Answers

You can access parameters on the Request object using one of the following methods:

$req->strTable;

// or

$req->input('strTable');

The input method also accepts a second parameter which will be used as the default return value if the key is not present in the Request.

If you want to check whether or not the Request contains a value before you attempt to access it, you can use filled:

if ($req->filled('strTable')) {
  // The request contains a value
}

Turns out that the way I had set up was in fact working and retrieving data:

$req->post('strTable');

The problem was in how I was testing it. In postman, there are several options to configure:

  • form-data
  • x-www-form-urlencoded
  • raw
  • binary

I had already switched to x-www-form-urlencoded to test it, but I forgot to fill the “key” and “value” information again. I didn’t realize that the fields blank as we switch between them.

Postman Configuration

Summing it up: It works when x-www-form-urlencoded selected but doesn’t work with form-data selected. Don’t know what the difference between them yet, but I’ll research it further.

By the way, it worked also with the suggestion from Rube Hart:

Related