0

I am sending a request to my codegniter app. When I vardump the object it clearly says its an object with a property name of subtotal. But when i try to access the property i get the error message "Trying to get property 'subtotal' of non-object"

This only happens with code igniter 4 and not 3. Not really sure why. Below is the code i am using. Any help would be really appreciated.

public function get_sales_tax(){
    $object = json_decode(file_get_contents("php://input"));
    var_dump($object->subtotal);
}

erorr message

CRITICAL - 2021-03-13 13:37:39 --> Trying to get property 'subtotal' of non-object #0 /Applications/MAMP/htdocs/simplemenu-all-stores/project-root/app/Controllers/Home.php(43): CodeIgniter\Debug\Exceptions->errorHandler(8, 'Trying to get p...', '/Applications/M...', 43, Array) #1 /Applications/MAMP/htdocs/simplemenu-all-stores/project-root/vendor/codeigniter4/framework/system/CodeIgniter.php(918): App\Controllers\Home->get_sales_tax() #2 /Applications/MAMP/htdocs/simplemenu-all-stores/project-root/vendor/codeigniter4/framework/system/CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Home)) #3 /Applications/MAMP/htdocs/simplemenu-all-stores/project-root/vendor/codeigniter4/framework/system/CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false) #4 /Applications/MAMP/htdocs/simplemenu-all-stores/project-root/public/index.php(45): CodeIgniter\CodeIgniter->run() #5 {main}

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
json
  • 1
  • 1
  • Does this answer your question? [Trying to get property of non-object in](https://stackoverflow.com/questions/5891911/trying-to-get-property-of-non-object-in) – ArSeN Mar 13 '21 at 19:57
  • Are you sure you're sending valid json data? `json_decode()` returns `false` if it can't decode the string as json. Can you do a `var_dump(file_get_contents('php://input'));` in the top of your `get_sales_tax()`-method and check what the request actually contains? Without knowing what the data actually looks like, we can't possibly know where it goes wrong or why. – M. Eriksson Mar 13 '21 at 21:12

0 Answers0