{"id":1571,"date":"2022-08-30T15:17:38","date_gmt":"2022-08-30T15:17:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/unhandled-exception-creating-default-object-from-empty-value-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:38","modified_gmt":"2022-08-30T15:17:38","slug":"unhandled-exception-creating-default-object-from-empty-value-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/unhandled-exception-creating-default-object-from-empty-value-collection-of-common-programming-errors\/","title":{"rendered":"&ldquo;Unhandled Exception: Creating default object from empty value&rdquo;-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to show a snippet of code and in the same time display comments that you can make to the snippets (I call the snippets code). But I&#8217;m having troubles to view the username of the user who made each and every comment. I expect to have each user when I&#8217;m calling <code>with(array('user', 'comments', 'comments.user'))<\/code> but I just get an unhandled exception.<\/p>\n<pre><code>\/\/Models\nclass Code extends Eloquent \n{\n    public function user() {\n        return $this-&gt;belongs_to('User');\n    }\n\n    public function comments() {\n        return $this-&gt;has_many('Comment');\n    }\n}\n\nclass Comment extends Eloquent\n{\n    public function user() {\n        $this-&gt;belongs_to('User');\n    }\n\n    public function code() {\n        $this-&gt;belongs_to('Code');\n    }\n}\n\nclass User extends Eloquent \n{\n    public function codes() {\n        return $this-&gt;has_many('Code');\n    }\n\n    public function comments() {\n        return $this-&gt;has_many('Comment');\n    }\n}\n\/\/Controller\n    public function get_show($id)\n    {\n        \/\/$code = Code::with(array('user', 'comments'))-&gt;where_slug($id)-&gt;first();\n        \/\/$comments = Comment::with(array('user'))-&gt;where_code_id($id)-&gt;get();\n        $code = Code::with(array('user', 'comments', 'comments.user'))-&gt;where_slug($id)-&gt;first();\n        dd($code);\n        \/\/return View::make('code.show')-&gt;with(array('code' =&gt; $code));\n    }   \n\n\/\/View\n    <\/code><\/pre>\n<h2><code>{{ $code-&gt;title }}<\/code><\/h2>\n<pre>\n    @include('code.controls')\n    <br \/>Syntax: {{ $code-&gt;syntax}} \n    <br \/><\/pre>\n<pre class=\"prettyprint linenums\"><code><code class=\"language-{{ $code-syntax }}\">{{ $code-&gt;content }}<\/code><\/code><\/pre>\n<pre>\n    @foreach($code-&gt;comments as $comment)\n        \n        <br \/>#{{ $comment-&gt;id }}\n        {{ User::find($comment-&gt;user_id)-&gt;first()-&gt;name }}\n        <br \/>{{ $comment-&gt;body }}\n        \n    @endforeach\n\n\/\/Message when trying to view\nUnhandled Exception\nMessage:\n\nCreating default object from empty value\n\nLocation:\n\n\/home\/victor\/Projects\/check-my-code\/laravel\/database\/eloquent\/query.php on line 167\n\nStack Trace:\n\n#0 \/home\/victor\/Projects\/check-my-code\/laravel\/laravel.php(40): Laravel\\Error::native(2, 'Creating defaul...', '\/home\/victor\/Pr...', 167)\n#1 \/home\/victor\/Projects\/check-my-code\/laravel\/database\/eloquent\/query.php(167): Laravel\\{closure}(2, 'Creating defaul...', '\/home\/victor\/Pr...', 167, Array)\n#2 \/home\/victor\/Projects\/check-my-code\/laravel\/database\/eloquent\/query.php(140): Laravel\\Database\\Eloquent\\Query-&gt;load(Array, 'user', NULL)\n#3 \/home\/victor\/Projects\/check-my-code\/laravel\/database\/eloquent\/query.php(74): Laravel\\Database\\Eloquent\\Query-&gt;hydrate(Object(Comment), Array)\n#4 \/home\/victor\/Projects\/check-my-code\/laravel\/database\/eloquent\/query.php(186): Laravel\\Database\\Eloquent\\Query-&gt;get()\n#5 \/home\/victor\/Projects\/check-my-code\/laravel\/database\/eloquent\/query.php(140): Laravel\\Database\\Eloquent\\Query-&gt;load(Array, 'comments', NULL)\n#6 \/home\/victor\/Projects\/check-my-code\/laravel\/database\/eloquent\/query.php(74): Laravel\\Database\\Eloquent\\Query-&gt;hydrate(Object(Code), Array)\n#7 \/home\/victor\/Projects\/check-my-code\/application\/controllers\/codes.php(31): Laravel\\Database\\Eloquent\\Query-&gt;get()\n#8 [internal function]: Codes_Controller-&gt;get_show('testar-snippets')\n#9 \/home\/victor\/Projects\/check-my-code\/laravel\/routing\/controller.php(325): call_user_func_array(Array, Array)\n#10 \/home\/victor\/Projects\/check-my-code\/laravel\/routing\/controller.php(285): Laravel\\Routing\\Controller-&gt;response('show', Array)\n#11 \/home\/victor\/Projects\/check-my-code\/laravel\/routing\/controller.php(165): Laravel\\Routing\\Controller-&gt;execute('show', Array)\n#12 \/home\/victor\/Projects\/check-my-code\/laravel\/routing\/route.php(153): Laravel\\Routing\\Controller::call('codes@show', Array)\n#13 \/home\/victor\/Projects\/check-my-code\/laravel\/routing\/route.php(124): Laravel\\Routing\\Route-&gt;response()\n#14 \/home\/victor\/Projects\/check-my-code\/laravel\/laravel.php(125): Laravel\\Routing\\Route-&gt;call()\n#15 \/home\/victor\/Projects\/check-my-code\/public\/index.php(34): require('\/home\/victor\/Pr...')\n#16 {main}\n<\/pre>\n<ol>\n<li>\n<p>The methods in the class Comment is missing return statements.<\/p>\n<pre><code>class Comment extends Eloquent\n{\n    public function user() {\n        $this-&gt;belongs_to('User');\n    }\n\n    public function code() {\n        $this-&gt;belongs_to('Code');\n    }\n}\n<\/code><\/pre>\n<p><strong>Should be<\/strong><\/p>\n<pre><code>class Comment extends Eloquent\n{\n    public function user() {\n        return $this-&gt;belongs_to('User');\n    }\n\n    public function code() {\n        return $this-&gt;belongs_to('Code');\n    }\n}\n<\/code><\/pre>\n<p>Got help from a thread on the Laravel forums: http:\/\/forums.laravel.io\/viewtopic.php?id=6266<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 11:52:39. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to show a snippet of code and in the same time display comments that you can make to the snippets (I call the snippets code). But I&#8217;m having troubles to view the username of the user who made each and every comment. I expect to have each user when I&#8217;m calling with(array(&#8216;user&#8217;, &#8216;comments&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,1],"tags":[],"class_list":["post-1571","post","type-post","status-publish","format-standard","hentry","category-laravel","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1571"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1571\/revisions"}],"predecessor-version":[{"id":8875,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1571\/revisions\/8875"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}