Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

As you can see, responses() does not return an instance of your model, rather the Response Facade. You should either rename your model or set a namespace for reference in your Ticket model.

Response.php

<?php namespace My\Models;

class Response extends \Eloquent {}

Ticket.php

<?php namespace My\Models;

class Ticket extends \Eloquent
{
    public function responses()
    {
        return $this->hasMany('My\Models\Response');
    }
}

Now $ticket->responses() returns an instance of your model.

Last updated 2 years ago.
0

ChrisRM said:

As you can see, responses() does not return an instance of your model, rather the Response Facade. You should either rename your model or set a namespace for reference in your Ticket model.

Response.php

<?php namespace My\Models;

class Response extends \Eloquent {}

Ticket.php

<?php namespace My\Models;

class Ticket extends \Eloquent
{
   public function responses()
   {
       return $this->hasMany('My\Models\Response');
   }
}

Now $ticket->responses() returns an instance of your model.

Thanks. I tried both solutions and they both worked. However, I'm curious why at first responses() couldn't return the instance of my Response model.

Thanks.

Last updated 2 years ago.
0

Michaelzlies said:

Thanks. I tried both solutions and they both worked. However, I'm curious why at first responses() couldn't return the instance of my Response model.

Thanks.

Response is actually a Laravel Facade, so the naming clashed. Keep that in mind when creating Models and Controllers with no namespacing.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.