0

I'm working on my Laravel project, and have a problem with many-to-many relationship
it throws back the following error:

BadMethodCallException Call to undefined method App\Models\Article::categories()

although I have defined the relationship as categories in my Article model, Laravel solution is:
Bad Method Call Did you mean App\Models\Article::categories()?

Please help me to find out which part of code could lead to this issue

Article Model: Article Model

Article Controller: Article Controller

Category Controller: Category Controller

Hafez Divandari
  • 8,381
  • 4
  • 46
  • 63
siavash
  • 11
  • 1
  • 4

1 Answers1

1

The categories method in the Article model is protected. To access it like you intend it needs to be declared as public.

class Article extends Model
{
    public function categories()
    {
        return $this->belongsToMany(Category::class);
    }
}
Dan
  • 5,140
  • 2
  • 15
  • 30
  • public function scopeKadwal($query) { return $query->where('id',1); } $share = Share::kadwal()->get(); error: Bad Method Call Did you mean App\Models\Share::scopeKadwal() ? I am encountering this error from last days, I tried several ways but it seems does working – MANSOOR KOCHY Mar 02 '22 at 04:52
  • Please open a separate question for your problem. It is entirely unrelated to my answer. – Dan Mar 02 '22 at 11:43