I would suspect that you are matching a different Route; is there any other URI that is similar, perhaps with a wildcard segment (/admin/{something})? If you have something like this, move it below your API Resource Route.
Jan 22, 2025
9
Level 3
apiResource is ignoring PUT, DELETE
I am using apiReource but I am getting this error whenever I try to delete using it:
"message": "The DELETE method is not supported for route api/v1/admin/company. Supported methods: GET, HEAD, POST.",
and here's my files: api.php
Route::apiResource("/admin/company", AdminCompanyController::class)
->middleware("authAdmin");
AdminCompanyController
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CompanyStoreRequest;
use App\Http\Requests\CompanyUpdateRequest;
use App\Http\Resources\CompanyShowResource;
use App\Models\Company;
use Illuminate\Support\Facades\Storage;
class AdminCompanyController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return CompanyShowResource::collection(Company::paginate(50));
}
/**
* Store a newly created resource in storage.
*/
public function store(CompanyStoreRequest $request)
{
$validated = $request->validated();
$data = [
'c_name' => $validated['name'],
'c_owner' => $validated['owner'],
'c_address' => $validated['address'],
'c_email' => $validated['email'],
'c_website' => $validated['website'],
'c_phone' => $validated['phone'],
'c_password' => bcrypt($validated['password']),
];
#----- Company Logo Download [START] -----#
if ($request->hasFile('logo')) {
$file = $request->file('logo');
$extension = $file->getClientOriginalExtension();
$filename = $validated["name"] . time() . '.' . $extension;
Storage::putFileAs(
'public/companies/logo',
$file,
$filename
);
$data['c_logo'] = $filename;
}
#----- Company Logo Download [END] -----#
Company::create($data);
return response()->json(['message' => 'Company created successfully'], 201);
}
/**
* Display the specified resource.
*/
public function show(Company $company)
{
return new CompanyShowResource($company);
}
/**
* Update the specified resource in storage.
*/
public function update(CompanyUpdateRequest $request, Company $company)
{
// Validate the request data
$validated = $request->validated();
// Prepare the data for updating
$data = [
'c_name' => $validated['name'],
'c_owner' => $validated['owner'],
'c_address' => $validated['address'],
'c_email' => $validated['email'],
'c_website' => $validated['website'],
'c_phone' => $validated['phone'],
];
// Update the password if it's provided
if (isset($validated['password'])) {
$data['c_password'] = bcrypt($validated['password']);
}
// Handle logo upload
if ($request->hasFile('logo')) {
// Delete the old logo if it exists
if ($company->c_logo && file_exists(public_path('uploads/company/' . $company->c_logo))) {
unlink(public_path('uploads/company/' . $company->c_logo));
}
// Upload the new logo
$file = $request->file('logo');
$extension = $file->getClientOriginalExtension();
$filename = time() . '.' . $extension;
$file->move('uploads/company/', $filename);
$data['c_logo'] = $filename;
}
// Update the company with the new data
$company->update($data);
return response()->json(['message' => 'Company updated successfully'], 200);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Company $company)
{
if ($company->c_logo && file_exists(public_path('uploads/company/' . $company->c_logo))) {
unlink(public_path('uploads/company/' . $company->c_logo));
}
$company->delete();
return response()->json(['message' => 'Company deleted successfully'], 200);
}
}
and app.php in bootstrap:
<?php
use App\Http\Middleware\AdminAuthMiddleware;
use App\Http\Middleware\CompanyAuthMiddleware;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
then: function () {
Route::middleware('api')
->prefix('api/v1')
->group(base_path('routes/api/v1/api.php'));
}
)
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
"authAdmin"=>AdminAuthMiddleware::class,
"authCompany"=>CompanyAuthMiddleware::class
]);
})
->withExceptions(function (Exceptions $exceptions) {
})->create();
why am I getting this error while I am not using except() method in apiResource?
Level 104
@foxdevuz ok... thinking that maybe you don't have an ID for the DELETE or UPDATE actions; likely you have null where it should be an ID, so you DELETE or PUT to api/v1/admin/company, not api/v1/admin/company/{company}
Confirm please what URL is being used?
Please or to participate in this conversation.