t0berius's avatar
Level 13

Call to undefined method App\\Models\\Order::mapInto() laravel API Resource

Assume my controller action:

    return OrderCollection::collection(Order::filter($filters)->paginate());

OrderCollection

<?php

namespace App\Http\Resources\Api\V1;

use Illuminate\Http\Resources\Json\ResourceCollection;

class OrderCollection extends ResourceCollection
{
	public function paginationInformation($request, $paginated, $default)
	{
	    $default['links']['custom'] = 'https://example.com';

	    return $default;
	}
}

OrderResource

class OrderResource extends JsonResource
{
	public function toArray(Request $request): array
	{
	    return [
	        'id' => $this->uuid,
	        'attributes' => [
	            'status_id' => $this->status_id->name,
	            'updatedAt' => $this->updated_at
	        ],
	    ];
	}
}

My main goal using the OrderCollection was to be able to override the pagination details using paginationInformation().

Receiving this error:

"message": "Call to undefined method App\Models\Order::mapInto()",
"exception": "BadMethodCallException",
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php",

Not able to figure out where things go wrong.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The error you're encountering, "Call to undefined method App\Models\Order::mapInto()", typically occurs when Laravel tries to call a method that doesn't exist on the Order model. This can happen if there's a mismatch between the expected type and the actual type being passed around in your code.

In your case, the issue seems to be related to how you're using the OrderCollection and OrderResource. The OrderCollection::collection method expects a collection of OrderResource instances, but it seems like it's not getting that.

Here's a step-by-step solution to resolve this issue:

  1. Ensure OrderCollection is using OrderResource correctly:

    Modify your OrderCollection to use OrderResource for each item in the collection.

    namespace App\Http\Resources\Api\V1;
    
    use Illuminate\Http\Resources\Json\ResourceCollection;
    
    class OrderCollection extends ResourceCollection
    {
        public function toArray($request)
        {
            return [
                'data' => $this->collection->map(function ($order) {
                    return new OrderResource($order);
                }),
            ];
        }
    
        public function paginationInformation($request, $paginated, $default)
        {
            $default['links']['custom'] = 'https://example.com';
    
            return $default;
        }
    }
    
  2. Update your controller to use OrderCollection directly:

    Instead of using OrderCollection::collection, you should return an instance of OrderCollection directly.

    use App\Http\Resources\Api\V1\OrderCollection;
    use App\Models\Order;
    
    class OrderController extends Controller
    {
        public function index(Request $request)
        {
            $filters = $request->all(); // Assuming you have some filters
            $orders = Order::filter($filters)->paginate();
    
            return new OrderCollection($orders);
        }
    }
    
  3. Ensure your OrderResource is correctly defined:

    Your OrderResource looks fine, but make sure it extends JsonResource and is correctly namespaced.

    namespace App\Http\Resources\Api\V1;
    
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class OrderResource extends JsonResource
    {
        public function toArray($request)
        {
            return [
                'id' => $this->uuid,
                'attributes' => [
                    'status_id' => $this->status_id->name,
                    'updatedAt' => $this->updated_at,
                ],
            ];
        }
    }
    

By following these steps, you ensure that the OrderCollection is correctly wrapping each Order model instance in an OrderResource, and the pagination information is correctly overridden.

This should resolve the "Call to undefined method App\Models\Order::mapInto()" error and allow you to customize the pagination details as needed.

Please or to participate in this conversation.