Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

angelorigo's avatar

How to reference correctly a BadMethodCallException ?

Hi,

How can i track this error below and solve it? how to reference correctly the method?

BadMethodCallException Call to undefined method Illuminate\Database\Eloquent\Builder::toArray()

use Illuminate\Http\Resources\Json\JsonResource;

class MyResource extends JsonResource { /** * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { parent::toArray($request);

0 likes
3 replies
LaryAI's avatar
Level 58

The error you're encountering, BadMethodCallException: Call to undefined method Illuminate\Database\Eloquent\Builder::toArray(), typically occurs when you're trying to call a method on an Eloquent query builder instance that doesn't exist. In this case, it seems like you're trying to call toArray() on a query builder instead of an Eloquent model or collection.

Here's how you can resolve this issue:

  1. Ensure You're Working with a Model or Collection: The toArray() method is available on Eloquent models and collections, not on the query builder. Make sure that you're calling toArray() on an actual model instance or a collection of models.

  2. Fetch the Model or Collection: If you're currently working with a query builder, you need to execute the query to get the model or collection. You can do this by using methods like get(), first(), or find().

Here's an example of how you might adjust your code:

use Illuminate\Http\Resources\Json\JsonResource;

class MyResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        // Assuming $this->resource is a query builder, fetch the model or collection
        $modelOrCollection = $this->resource->get(); // or ->first(), ->find($id), etc.

        // Now you can safely call toArray() on the model or collection
        return $modelOrCollection->toArray();
    }
}
  1. Check Your Resource Initialization: Ensure that when you're creating an instance of MyResource, you're passing a model or collection, not a query builder. For example:
// Correct usage
$resource = new MyResource(MyModel::find($id)); // or MyModel::all(), etc.

By ensuring that you're working with the correct type of object, you should be able to avoid the BadMethodCallException.

tisuchi's avatar

@angelorigo I assume you are referring to the production system!

You can use some freemium server called Sentry.

In addition, you can check in laravel.log file to see the details of the error.

2 likes
martinbean's avatar

@angelorigo Instead of spending time and effort trying to “track” errors, you should instead just spend that time and effort fixing the actual problem.

If you’re getting a BadMethodCallException then it will tell you the file and line number it’s occurring. The error message is telling you that you’re calling toArray on a query builder instance. So fix the bug that is at that location by either returning the object you were expecting instead of a query builder instance, or to call the actual method you intended instead of toArray.

1 like

Please or to participate in this conversation.