0

I have field names in my model that are numbers (I don't have control over it's names). When I try to get values from my model I get an Exception, before having those fields everything was fine

This worked fine till the number field were added.

Model::find($id)->name;

I get Trying to get property of non-object

I tried to hide them from my JSON with the protected hidden array but neither '0', 0 or '{0}'seems to work.

This is the error

{
"errors": "Sorry, something went wrong.",
"exception": "Symfony\\Component\\Debug\\Exception\\FatalErrorException",
"message": "Uncaught exception 'ErrorException' with message 'Trying to get property of non-object' in /home/david/workspace/papw2/jukebox/api/app/AlbumComment.php:18\nStack trace:\n#0 /home/david/workspace/papw2/jukebox/api/app/AlbumComment.php(18): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(8, 'Trying to get p...', '/home/david/wor...', 18, Array)\n#1 /home/david/workspace/papw2/jukebox/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2669): App\\AlbumComment->getUserAttribute(NULL)\n#2 /home/david/workspace/papw2/jukebox/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2681): Illuminate\\Database\\Eloquent\\Model->mutateAttribute('user', NULL)\n#3 /home/david/workspace/papw2/jukebox/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2435): Illuminate\\Database\\Eloquent\\Model->mutateAttributeForArray('user', NULL)\n#4 /home/david/workspace/papw2/jukebox/api/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(2379): Illuminate\\Dat",
"trace": []

}

What is the correct syntax for this?

Vector
  • 747
  • 2
  • 11
  • 22
  • Identifiers may begin with a digit but unless quoted may not consist solely of digits. Try using back ticks like ` 0 `: – Emeka Mbah May 11 '15 at 08:28
  • 1
    You experience two different problems here. One being `Trying to get property of non-object`. This is probably caused by `Model::find($id)` returning `null`. The other problem are your numeric column names. It's currently not possible to hide them and other features might not work as well. This is mostly due to how PHP is really losely typed... – lukasgeiter May 12 '15 at 13:32
  • So, it's not possible unless I hack laravel. Thanks. – Vector May 12 '15 at 17:36
  • Vector dough you accepted my answer I want to know if the hack worked for you. I open an issue https://github.com/laravel/framework/issues/8710 and I hope big Boss @lukasgeiter can assist :) – Emeka Mbah May 13 '15 at 13:10
  • I worked around it and did a refactor so there's no more numbered columns. Maybe this weekend I could test it. – Vector May 14 '15 at 03:15

1 Answers1

1

UPDATED

Presently this is not possible in Laravel as seen in this line of code located in vendor\symfony\var-dumper\Symfony\Component\VarDumper\Cloner\VarCloner.php at line 74.


  if ($zval['zval_isref'] = $queue[$i][$k] === $cookie) {
                        $zval['zval_hash'] = $v instanceof Stub ? spl_object_hash($v) : null;
                    }

However if you need a hack you can replace the code above in VarCloner.php with:


  if ($zval['zval_isref'] = (isset($queue[$i][$k])) ? ($queue[$i][$k] === $cookie) : false) {
                        $zval['zval_hash'] = $v instanceof Stub ? spl_object_hash($v) : null;
                    }
Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96