[SOLVED] Extracting a Json Object using Laravel

Issue

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

Solution

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'];

Answered By – IGP

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.