user7856768
user7856768

Reputation:

Laravel DELETE method with request body

I've been trying to add a FormRequest with rules and message to my delete method, but the request is coming back empty and the rules are failing every time.

Is it possible to get the request data in a delete method?

Here's my request class:

use App\Http\Requests\Request;

class DeleteRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'staff_id' => ['required', 'exists:users,uid'],
            'reason' => ['required', 'string'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'staff_id.required' => staticText('errors.staff_id.required'),
            'staff_id.exists' => staticText('errors.staff_id.exists'),
            'reason.required' => staticText('errors.reason.required'),
            'reason.string' => staticText('errors.reason.string'),
        ];
    }
}

And the controller:

/**
 * Handle the 'code' delete request.
 *
 * @param integer $id            The id of the code to fetch.
 * @param DeleteRequest $request The request to handle the data.
 * @return response
 */
public function deleteCode($id, DeleteRequest $request)
{
    dd($request->all());
}

Upvotes: 6

Views: 10504

Answers (1)

sepehr
sepehr

Reputation: 18455

Even though the HTTP/1.1 spec does not explicitly state that DELETE requests should not have an entity body, some implementations completely ignore the body which contains your data, e.g. some versions of Jetty and Tomcat. On the other hand, some clients do not support sending it as well.

Think of it as a GET request. Have you seen any with form data? DELETE requests are almost the same.

You can read a LOT on the subject. Start here:
RESTful Alternatives to DELETE Request Body

It seems like that you want to alter the state of the resource rather than destroying it. Soft-deleting is not deleting and thus requires either a PUT or a PATCH method which both support entity bodies. If soft-deleting is not the case, you're doing two operations through one call.

Upvotes: 16

Related Questions