“Unhandled Exception: Creating default object from empty value”-Collection of common programming errors

I’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’m having troubles to view the username of the user who made each and every comment. I expect to have each user when I’m calling with(array('user', 'comments', 'comments.user')) but I just get an unhandled exception.

//Models
class Code extends Eloquent 
{
    public function user() {
        return $this->belongs_to('User');
    }

    public function comments() {
        return $this->has_many('Comment');
    }
}

class Comment extends Eloquent
{
    public function user() {
        $this->belongs_to('User');
    }

    public function code() {
        $this->belongs_to('Code');
    }
}

class User extends Eloquent 
{
    public function codes() {
        return $this->has_many('Code');
    }

    public function comments() {
        return $this->has_many('Comment');
    }
}
//Controller
    public function get_show($id)
    {
        //$code = Code::with(array('user', 'comments'))->where_slug($id)->first();
        //$comments = Comment::with(array('user'))->where_code_id($id)->get();
        $code = Code::with(array('user', 'comments', 'comments.user'))->where_slug($id)->first();
        dd($code);
        //return View::make('code.show')->with(array('code' => $code));
    }   

//View
    

{{ $code->title }}

    @include('code.controls')
    
Syntax: {{ $code->syntax}}
{{ $code->content }}
    @foreach($code->comments as $comment)
        
        
#{{ $comment->id }} {{ User::find($comment->user_id)->first()->name }}
{{ $comment->body }} @endforeach //Message when trying to view Unhandled Exception Message: Creating default object from empty value Location: /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php on line 167 Stack Trace: #0 /home/victor/Projects/check-my-code/laravel/laravel.php(40): Laravel\Error::native(2, 'Creating defaul...', '/home/victor/Pr...', 167) #1 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(167): Laravel\{closure}(2, 'Creating defaul...', '/home/victor/Pr...', 167, Array) #2 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(140): Laravel\Database\Eloquent\Query->load(Array, 'user', NULL) #3 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(74): Laravel\Database\Eloquent\Query->hydrate(Object(Comment), Array) #4 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(186): Laravel\Database\Eloquent\Query->get() #5 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(140): Laravel\Database\Eloquent\Query->load(Array, 'comments', NULL) #6 /home/victor/Projects/check-my-code/laravel/database/eloquent/query.php(74): Laravel\Database\Eloquent\Query->hydrate(Object(Code), Array) #7 /home/victor/Projects/check-my-code/application/controllers/codes.php(31): Laravel\Database\Eloquent\Query->get() #8 [internal function]: Codes_Controller->get_show('testar-snippets') #9 /home/victor/Projects/check-my-code/laravel/routing/controller.php(325): call_user_func_array(Array, Array) #10 /home/victor/Projects/check-my-code/laravel/routing/controller.php(285): Laravel\Routing\Controller->response('show', Array) #11 /home/victor/Projects/check-my-code/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('show', Array) #12 /home/victor/Projects/check-my-code/laravel/routing/route.php(153): Laravel\Routing\Controller::call('codes@show', Array) #13 /home/victor/Projects/check-my-code/laravel/routing/route.php(124): Laravel\Routing\Route->response() #14 /home/victor/Projects/check-my-code/laravel/laravel.php(125): Laravel\Routing\Route->call() #15 /home/victor/Projects/check-my-code/public/index.php(34): require('/home/victor/Pr...') #16 {main}
  1. The methods in the class Comment is missing return statements.

    class Comment extends Eloquent
    {
        public function user() {
            $this->belongs_to('User');
        }
    
        public function code() {
            $this->belongs_to('Code');
        }
    }
    

    Should be

    class Comment extends Eloquent
    {
        public function user() {
            return $this->belongs_to('User');
        }
    
        public function code() {
            return $this->belongs_to('Code');
        }
    }
    

    Got help from a thread on the Laravel forums: http://forums.laravel.io/viewtopic.php?id=6266

Originally posted 2013-11-27 11:52:39.