Tag Archives: Model Error

[Solved] Model Error: must return a relationship instance

Model Error: must return a relationship instance

There is a method in the model as follows:

public function adjustbills()
    {
        if($this->user_combined == '1'){
            $invoiceNumberArr = ['xxx'];
            return Adjustbill::whereIn('invoice_number',$invoiceNumberArr)->where('user_id', $this->user_id);
        }else{
            return $this->hasMany(Adjustbill::class, 'invoice_number', 'invoice_number')->where('user_id', $this->user_id);
        }
    }

When the calling method is model -> When adjustbills,

  1. If it goes to the following judgment, no error will be reported
  2. If it goes to the above judgment, it will report an error must return a relationship instance, because laravel uses the following hasMany relationship by default, if not, it will report an error.

Solution:

  1. Find a way to modify the above logic to the following way of relationship
  2. Called as :model->adjustbills->get()