Reputation: 790
I am working along with @Jeffrey_way series of Laracasts Many to Many Relations (With Tags)
Below is the code I have written in CMD using Laravel Tinker:
After executing the last line of code ($article->tags()->toArray();
Although everything seems to be OK with my code but still I get following error:
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::toArray()'
Upvotes: 10
Views: 30150
Reputation: 5168
I had the same problem and solved it by adding get()
For example:
$article->tags()->get()->toArray();
Upvotes: 7
Reputation: 3681
Try this instead:
$article->tags()->all()->toArray();
Underlying the tags()
is probably a Query\Builder
object which represents a query that has not yet run. Instead you need a Collection object which is a query that has run, on which to call toArray()
. ->all()
is one such call that converts a query builder into a collection by actually running the query.
Upvotes: 0
Reputation: 184
If you want to actually "get" relational data, you don't put parenthesis arount tags
. This will work just fine:
$article->tags->toArray();
You put parenthesis when you need to "query" to that collection (Ex. sync, save, attach).
Reference: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many
Upvotes: 7