Home > other >  Extracting a Json Object using Laravel
Extracting a Json Object using Laravel

Time:03-22

I have a Collection if I try

$plan=Plan::where('id',15)->first() I get

    App\Plan {#3974
     id: 15,
     title: "{"en":"title eng","ar":"gf"}",
     sub_title: "{"en":"sub title eng","ar":"gfh"}",
     description: "{"en":"des eng","ar":"g"}",
     created_at: "2022-03-17 15:54:59",
     updated_at: "2022-03-17 15:54:59",}

I'm trying to get the title as it is to pass it but whenever I try

$plan['title'] 

I only get title eng I'm expecting to get {"en":"title eng","ar":"gf"} I tried json_decode it returned null

CodePudding user response:

Since you seem to have a getter that returns the correct title, in order to get the raw, original attribute you'll need to use the getAttributes method.

$plan->getAttributes()['title'];
  • Related