Vector
Vector

Reputation: 757

Hide number field from Eloquent model in Laravel

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?

Upvotes: 0

Views: 761

Answers (1)

Emeka Mbah
Emeka Mbah

Reputation: 17563

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;
                    }

Upvotes: 1

Related Questions