Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()'
like image 622
Hashmatullah Noorzai Avatar asked Jun 14 '16 06:06

Hashmatullah Noorzai


2 Answers

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

like image 186
YigitOzkavci Avatar answered Oct 20 '22 15:10

YigitOzkavci


I had the same problem and solved it by adding get()

For example:

$article->tags()->get()->toArray();
like image 43
Brad Ahrens Avatar answered Oct 20 '22 15:10

Brad Ahrens