Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mihirpatel83's avatar

Passing object to function not typecasting

I have a project model object where in there is a json field named as "attributes". When I print_r($project->attributes), It prints the typecasted array of attributes(MySql column) field as below:

Array ( [1] => Array ( [value] => Test ) [2] => Array ( [value] => 1 ) [3] => Array ( [value] => 2 ) [4] => Array ( [value] => 2 ) [5] => Array ( [value] => 2019-07-25 ) [6] => Array ( [value] => 2019-07-25 ) [7] => Array ( [value] => 15 ) [8] => Array ( [value] => ) [10] => Array ( [value] => 50 ) [12] => Array ( [value] => Array ( [0] => 1 ) ) ) 

But when I pass the same object $project to a function and print_r($project->attributes) then it displays all the fields of the tables.

Array ( [id] => 1 [attributes] => {"1": {"value": "Test"}, "2": {"value": "1"}, "3": {"value": "2"}, "4": {"value": "2"}, "5": {"value": "2019-07-25"}, "6": {"value": "2019-07-25"}, "7": {"value": "15"}, "8": {"value": null}, "10": {"value": "50"}, "12": {"value": ["1"]}} ) 

Why is this happening? Changing the field name from "attributes" to anything else like "attributes1" then it works properly.

0 likes
2 replies
bobbybouwmann's avatar

What do you mean by passing it to a function?

In general Laravel will convert the model to the correct type casted outside the model itself. Inside the model you get the original values. What I'm missing some context to help you any further!

mihirpatel83's avatar

I will try to explain it in detail. I have a ProjectController and one of the action has code as below

foreach ($listAttributes AS $attribute) {  
            if(isset($project->attributes[$attribute['id']])){
                $col = 'col' . $attribute['id'];
                $val[]= $project[$col];            
            }   
        }

printing $project->attributes variable here before the for loop will give result as below. It basically returns the typecasted value of json field to array and where "attributes" is the json field of table.

Array ( [1] => Array ( [value] => Test ) [2] => Array ( [value] => 1 ) [3] => Array ( [value] => 2 ) [4] => Array ( [value] => 2 ) [5] => Array ( [value] => 2019-07-25 ) [6] => Array ( [value] => 2019-07-25 ) [7] => Array ( [value] => 15 ) [8] => Array ( [value] => ) [10] => Array ( [value] => 50 ) [12] => Array ( [value] => Array ( [0] => 1 ) ) ) 

Now like Project, I have many entities and each have an "attributes" field in database like Product, Event etc.

So I changed above code to

function getEntityValues($listAttributes,$entity){
    $tmp=[];
    foreach ($listAttributes AS $attribute) {  
        if(isset($entity->attributes[$attribute['id']])){
            $col = 'col' . $attribute['id'];
            $tmp[]= $entity[$col];            
        }   
    }
    return $tmp;
}   

which can be called like $val = Entity::getEntityValues($listAttributes,$project); //Project entity $val = Entity::getEntityValues($listAttributes,$event); //Event entity

Now, when I print $entity->attributes in the above function It returns

Array ( [id] => 1 [attributes] => {"1": {"value": "Test"}, "2": {"value": "1"}, "3": {"value": "2"}, "4": {"value": "2"}, "5": {"value": "2019-07-25"}, "6": {"value": "2019-07-25"}, "7": {"value": "15"}, "8": {"value": null}, "10": {"value": "50"}, "12": {"value": ["1"]}} ) 

This you can see that is a list of all fields returned in a collection and not just the "attributes" field. Now, If i change the attributes field in database to something else for eg "properties" then everything works fine but it's the issue only when the name of the field is "attributes". Is it because there is already a "attributes" property in collection?

Just trying to figure If is probably due to using any reserve word in Laravel like "attributes". If yes, then which are the other list of names that are reserved? I didn't find any such list though.

Please or to participate in this conversation.