mirsahib
3 months ago
229
2
Laravel

BadMethodCallException : Model function doesn't exist even though it is there

Posted 3 months ago by mirsahib

I have 5 table but i am currently concern with 2 table Tenant and Payment, Tenant table has one to many relation with Payment table (Tenant table is also belongs to House table)

class Tenant extends Model
{
    //
    protected $fillable = ['name','nid','nid_img','phone','exp_rent','paid_rent',
                            'dues','pay_date','comment','status','exit_date'];


                            

    public function house()
    {
        return $this->belongsTo(House::class);
    }

    public function payments()
    {
        return $this->hasMany(Payment::class);
    }
}

Here is the seeder function

public function run()
    {
        factory(App\House::class, 10)->create()->each(function ($house) {
            // Seed the relation with one address
            $tenant = factory(App\Tenant::class,5)->make();
            $mother = factory(App\MotherMeter::class)->make();
            $sub_meter = factory(App\SubMeter::class)->make();
            $payment = factory(App\Payment::class,5)->make();
            $house->tenants()->saveMany($tenant);
            $house->motherMeter()->save($mother);
            $tenant->payments()->saveMany($payment);
            $mother->sub_meters()->save($sub_meter);
        });
    }

I am getting this error even though the function payments exists in the Tenant model

 BadMethodCallException 

  Method Illuminate\Database\Eloquent\Collection::payments does not exist.

Please sign in or create an account to participate in this conversation.