I’m using laravel 10.x and everything was going good until now because with no reason i created a new models and all routes of the controller are working excepts delete, it can’t be reach from postman needer frontend with angular
I’m using the resource in my api.php but I also created an specific one for just delete but it’s still not working.
api.php:
Route::prefix('license')->group(function () {
Route::delete('companies/{company}', [LicenseCompanyController::class, 'destroy']);
Route::resource('companies', LicenseCompanyController::class);
Route::resource('licenses', LicenseController::class);
route::get('licenses/{license}', [LicenseController::class, 'updateAvailableQuantity']);
});
Controller:
<?php
namespace AppHttpControllers;
use AppModelsLicenseCompany;
use IlluminateHttpRequest;
class LicenseCompanyController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return LicenseCompany::all();
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'link' => 'required',
]);
$licenseCompany = LicenseCompany::create($request->all());
return response()->json($licenseCompany, 201);
}
/**
* Display the specified resource.
*/
public function show(LicenseCompany $company)
{
return response()->json($company);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, LicenseCompany $company)
{
$company->update($request->all());
return response()->json($company);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(LicenseCompany $company)
{
// $licenseCompany;
return response()->json(['message' => 'working'], 204);
// return response()->json($licenseCompany, 204);
}
}
Model:
class LicenseCompany extends Model
{
use HasFactory;
protected $fillable = [
'name',
'description',
'link',
'email',
'phone'
];
public function licenses()
{
return $this->hasMany(License::class);
}
}
The route like index and show are working correctly and delete is return 204 with nothing from postman neither an empty array
2
Answers
Now after trying more thing I just figured out that it's not possible to debug the destroy method because it never return a json or anything with https 204, and you just have to make it work and it will do averything, my problem was that in the resource route it was
**/{company}
but in the method I was waitingdestroy(Request LicenseCompany $licenseCompany)
but I changed this todestroy(LicenseCompany $company)
and without debugging everything is working fine nowTry calling just $id instead of the LicenseCompany class