PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 19, 2022

[FIXED] Laravel access JSON object in controller

 January 19, 2022     laravel     No comments   

Issue

I have this array in the controller after the submitted form holds in variable $product.

[
    {
        "id": 2,
        "name": "nancy",
        "cost": 34,
        "quantity": 0,
        "barcode": 12345,
        "category_id": 2,
        "created_at": "2020-07-05T16:04:10.000000Z",
        "updated_at": "2020-07-09T04:06:09.000000Z"
    },
    {
        "id": 5,
        "name": "jk",
        "cost": 100,
        "quantity": 2,
        "barcode": 147258,
        "category_id": 2,
        "created_at": "2020-07-08T20:34:56.000000Z",
        "updated_at": "2020-10-18T13:09:16.000000Z"
    }
]

How can I access the properties in objects like id, name, barcode ?


Solution

If you want to make it as array of array.

$products = json_decode($products, true);  // Return array of array.

foreach($products as $product){
    echo $product['name']; 
}


Answered By - Haseeb Javed
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing