pileup
pileup

Reputation: 3262

Page not found when working with apiResource

I have the following simple project:

App\Http\Controllers\ProductController.php which includes all the API methods (store, show, update, destroy)

App\Http\Resources\ProductResource.php which returns a JSON

App\Models\Product.php which just has the protected fillable array

and finally App\routes\api.php with the following code:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;


Route::apiResource('products', ProductController::class);

But when I try to POST some example product using Postman, I get 404 Not Found.

The URL I post to is localhost:8000/api/products.store, because from what I understand is that apiResource simply creates all the named routes in the form of products.method_name

*By the way, the URL localhost:8000/api/products returns an empty {"data":[]}

I'm clearly doing something wrong - what is it?

Upvotes: 1

Views: 332

Answers (2)

Paresh Lalvani
Paresh Lalvani

Reputation: 74

Please try to add Accept: application/json in header.

Upvotes: 0

Hanan Alhasan
Hanan Alhasan

Reputation: 300

you can't use the route name in the URL like this, the request should be like:

localhost:8000/api/products/store

try this command

 php artisan route:list

and you will see all your routes and the URL that you should use for each one

Upvotes: 1

Related Questions