0

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

4
  • You don't appear to have an array there, but an instance of App\Plan. So use object property access syntax, if those properties are public, and else, go look for what getter methods are available. Commented Mar 21, 2022 at 11:09
  • @CBroe [] or -> are valid in a collection furthermore $plan->title also returns 'title eng' Commented Mar 21, 2022 at 11:13
  • Then there's probably some "magic" involved there, that automatically picks the value out of the structure, that corresponds to the current language. Commented Mar 21, 2022 at 11:15
  • It looks like it's automatically translating the properties, so if your language is set to English, then $plan['title'] will show title eng, while if your language was set to AR, then it would show gf Commented Mar 21, 2022 at 12:58

1 Answer 1

1

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'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.