digitaldreams / laracrud

Laravel Code Generator based on MySQL Database
MIT License
331 stars 54 forks source link

Undefined property: App\Models\.... on line 101 in ...\lluminate\Database\Eloquent\Relations\BelongsTo.php #35

Closed c-myers1 closed 4 years ago

c-myers1 commented 4 years ago

With the command

php artisan laracrud:controller Issuance --api

I'm getting the error:

Undefined property: App\Models\Issuance::$weeklynetorderid on line 101 in C:\xampp2\htdocs\myapp\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\BelongsTo.php

weeklynetorderid is the foreign key pointing to an id column in another table. The function within the Issuance model is

public function weeklynetorderid()
{
    return $this->belongsTo(WeeklyNetorder::class,'weeklynetorderid');
}

I generated WeeklyNetorder model already which is being referred to.

My searches for this error seems to indicate when there is confusion between weeklynetorderid as a function or as a property but it hasn't made a solution clearer to me.

How can I troubleshoot?

c-myers1 commented 4 years ago

Ok, seems to be a Laravel issue as per this.

digitaldreams commented 4 years ago

Hi @c-myers1 Its not a laravel issue. Let me explain what was wrong on your above code. You can't your belongsTo method name same as your foreign key name. Here your belongsTo method name weeklynetorderid and foreign key weeklynetorderid which is same name.

Rename your belongsTo method name weeklynetorder instead of weeklynetorderid will solve your problem.