I have a project in Laravel 8 where I’ve created a controller with a update
method.
public function update(Product $product, Request $request)
{
$data = $request->all();
$validator = Validator::make($data, [
'name' => 'string',
'quantity' => 'integer',
//'image_path' => 'required|mimes:jpeg,bmp,png',
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors(), 'error']);
}
$product->fill($data);
$product->save();
return new ProductResource($product);
}
I’ve followed this answer to set up Phpstorm to debug Laravel and when I call that end-point with Postman the debug effectively starts.
Now, I am trying to use this endpoint, doing a js function that send the data of a html form.
From this answer, I wrote the following code
const productEditFormSubmit = async (e) => {
let formData = new FormData();
formData.append('fileData', e.target.elements.image.files[0]);
formData.append('code', e.target.elements.name.value);
formData.append('quantity', e.target.elements.code.value);
formData.append('name', e.target.elements.quantity.value);
try {
const apiUrl = `http://localhost:8000/api/product/${e.target.elements.code.value}`;
const options = {
method: 'PATCH',
body: formData
};
const response = await fetch(apiUrl, options);
const body = await response.json();
return body;
}
catch (error) {
console.log('(App) Error: ', error);
}
e.preventDefault();
}
However, the debug doesn’t start at all.
I’m sure the end-point is called and doesn’t crash midway, because if I alter it to send a “fake” response instead of return new ProductResource($product);
, I find it in the browser network table:
However, The update
doesn’t do its logic – I mean, the product isn’t correctly updated as it happens with postman call.
Now, I am in the blind, as I can’t see what is actually happening inside that function.
Any idea why the debugger doesn’t start in this case? I’ve also installed and tried to enable the Xdebug helper extension but that doesn’t change anything.
By the way, any other ways to check what happens in that function when called in the browser? Log, dump function, whatever?