'How to create a relationship in Laravel 9

I tried to create a relationship in Laravel for this,

  • Expense has May ExpensesItem
  • Expense has One ExpensesCategory

But I could not create a relationship between Expenses and ExpensesCategory. I tried hasOneThrough relationship but it is not compatible with this. Is this possible to create a relationship between Expenses and ExpensesCategory

    Expense
        id - integer 
     
    ExpensesItem
        id - integer 
        expenses_category_id - integer
     
    ExpensesCategory
        id - integer
        name - string 

I tried this, not working.

public function category()
    {
        return $this->hasOneThrough(ExpensesCategory::class, ExpensesItem::class,'expenses_category_id','id','id','id');
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source