1

I have a table which contain JSON in a column that I have to get as an object but when I pull data using laravel model then it obviously return as JSON or string which I have to decode every where, where I use this model (this is not dry concept) but the thing is if I pull one record and one place then it is only one execution of code and ok but what if I have to pull collection of the data and multiple place then I have to run same decode every time is there any way in laravel that can do for me instead of using self made loop

here is the table

+-----------------------------------------------------------------------------+
|                                   table                                     |
+-----------------------------------------------------------------------------+
|id | title | text                                                            |
+-----------------------------------------------------------------------------+
| 1 |   A1  | {"subject":"Subject1","word":"Lorem ipsum dolor sit amet, ..."} |
| 2 |   A2  | {"subject":"Subject2","word":"Lorem ipsum dolor sit amet, ..."} |
| 3 |   A3  | {"subject":"Subject3","word":"Lorem ipsum dolor sit amet, ..."} |
| 4 |   A4  | {"subject":"Subject4","word":"Lorem ipsum dolor sit amet, ..."} |
| 5 |   A5  | {"subject":"Subject5","word":"Lorem ipsum dolor sit amet, ..."} |
| 6 |   A6  | {"subject":"Subject6","word":"Lorem ipsum dolor sit amet, ..."} |
| 7 |   A7  | {"subject":"Subject7","word":"Lorem ipsum dolor sit amet, ..."} |
| 8 |   A8  | {"subject":"Subject8","word":"Lorem ipsum dolor sit amet, ..."} |
| 9 |   A9  | {"subject":"Subject9","word":"Lorem ipsum dolor sit amet, ..."} |
| 10|  A10  | {"subject":"Subject10","word":"Lorem ipsum dolor sit amet..."}  |
+-----------------------------------------------------------------------------+

i have tried this

$tables = TableModel::all();
foreach ($tables as &$table) {
    $table->text = json_decode($table->text);
}


Result what I want

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [title] => A1
            [text] => stdClass Object
                (
                    [subject] => Subject1
                    [word] => Lorem ipsum dolor sit amet...
                )

        )

    [1] => stdClass Object
        (
            [id] => 2
            [title] => A2
            [text] => stdClass Object
                (
                    [subject] => Subject2
                    [word] => Lorem ipsum dolor sit amet...
                )

        )

    ...
)

If is there any easy way to that which is more efficient and follow dry then share with me.

Thanks in advance.

1
  • i called model then loop through it and overwrite then element with object using json_decode() Commented Feb 25, 2017 at 21:31

1 Answer 1

1

You can use Attribute Casting concept to get the result in the desired format every time you fetch the record from database. To convert from JSON to Array you can use the following code.

protected $casts = [
    'text' => 'array',
];

As mention in the Official Documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) this helps me lot and saved the day

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.