Laravel 5.5 belongsTo 未按预期工作

Laravel 5.5 belongsTo not working as expected

我有三个模型有关系,ReservedTicket extends Ticket:

App/TicketType:

class TicketType extends Model
{
    protected $fillable = [
        'name', 'startdate', 'enddate', 'countinuous', 'price', 'description', 'personal', 'total', 'minage', 'maxage', 'event_id', 'header_image_path', 'footer_image_path',
    ];

    public function event()
    {
        return $this->belongsTo(Event::class);
    }

    public function tickets()
    {
        return $this->hasMany(Ticket::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

App/Ticket:

class Ticket extends Model
{
    protected $fillable = [
        'user_id', 'first_name', 'last_name', 'prefix_name', 'ticket_type_id', 'sold_on', 'sold_by', 'scanned_at', 'scanned_by',
    ];

    public function ticketType()
    {
        return $this->belongsTo(TicketType::class, 'ticket_type_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

App/ReservedTicket:

class ReservedTicket extends Ticket
{
    protected $table = 'reserved_tickets';
}

我已经像在我的应用程序中的任何其他模型上一样设置了关系。我什至尝试指定外键列,但它没有发生。

如果我使用 TicketTypehasMany(),我会得到所有 Tickets。但是反之则不行。

我是不是因为继承而遗漏了什么?我正在使用 ReservedTicket.

的实例

将 class 扩展到新模型时,您还需要在扩展 class 中定义关系

class ReservedTicket extends Ticket
{
    protected $table = 'reserved_tickets';

    public function ticketType()
    {
        return $this->belongsTo(TicketType::class, 'ticket_type_id');
    }

    public function user()
    {
         return $this->belongsTo(User::class);
    }
}