I am trying to update a resource in Laravel sending a PATCH request to my controller. This is my AJAX call
$.ajax('profile', { // Replace with your actual endpoint URL
type: 'PATCH',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'), // CSRF token for Laravel
// 'X-HTTP-Method-Override': 'PATCH'
},
data: {status: 'some status'},
success: function (data) {
console.log(data);
},
})
.catch((error) => {
console.error('Error:', error);
});
And currently my Controller simply gives back the request data as a response
public function update(ProfileUpdateRequest $request)
{
return response()->json(['message' => 'Data updated successfully', 'data' => $request->all()]);
}
I get the message 'Data updated sucessfully', the data array however, is completely empty.
I've already tried it with type: 'POST' and spoofing the method in the data array with _method: 'PATCH', which led to the message "'POST' is not a supported method for this route."
Any ideas?
EDIT: ProfileUpdateRequestClass:
namespace App\Http\Requests;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class ProfileUpdateRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
// 'name' => ['required', 'string', 'max:255'],
// 'email' => ['required', 'string', 'email', 'max:255'],
];
}
}