User97798
User97798

Reputation: 644

How to pull json data from data mysql as object using laravel5?

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.

Upvotes: 1

Views: 63

Answers (1)

nextt1
nextt1

Reputation: 3988

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.

Upvotes: 1

Related Questions