简体   繁体   中英

Laravel 5 Throw an exception: BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::tags()

I'm using Laravel 5, and I'm a bit new to this framework. I searched a lot for the problem, but in return I got nothing related.

So, I have two models: Article and Tag.

In Article Model I have a method like this:

public function tags() {
    return $this->belongsToMany("App\Tag");
}

And in my Tag Model I have a method like this:

public function articles() {
    return $this->belongsToMany("App\Article");
}

Now the thing is that when I'm testing this in tinker like this:

$article->tags()->attach(1);

It gives me the following Exception:

BadMethodCallException with message 'Call to undefined method Illuminate\\Database\\Query\\Builder::tags()'

But when I'm calling it like this:

$tag->articles()->attach(1);

It totally works like a charm and It doesn't throw any kind of Exception whatsoever.

I'm actually learning this stuff from Laracast and my Classes and methods and files are kind of like this: BadMethodCallException with message 'Call to undefined method Illuminate\\Database\\Query\\Builder::belongToMany()'

Except I think I don't have a typo problem.

Well, I got the answer on my own & just share it for others.

The code doesn't have any problem.

I should've just restarted the Tinker. It was a Tinker problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM