PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 5, 2022

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

 January 05, 2022     laravel, laravel-5, php     No comments   

Issue

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()'

Solution

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



Answered By - YigitOzkavci
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing