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:
-
Ensure
OrderCollectionis usingOrderResourcecorrectly:Modify your
OrderCollectionto useOrderResourcefor 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; } } -
Update your controller to use
OrderCollectiondirectly:Instead of using
OrderCollection::collection, you should return an instance ofOrderCollectiondirectly.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); } } -
Ensure your
OrderResourceis correctly defined:Your
OrderResourcelooks fine, but make sure it extendsJsonResourceand 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.