-2

I'm developing a laravel api using resource controller. There I have to use PUT method for updating something.

When I try to pass data through body with form-data the validation error happens. I have to pass the data through param.

Is this normal for laravel resource controller? If so why?

Here's my code.

public function update(RoleRequest $request, string $id)
{
     $data = Role::find($id);

     $new_data = [
         'name' => $request->name
     ];

     $data->update($new_data);

     return $this->apiResponse(true, 'role updated successfully', $data, 200);
}

Here's the validation error.

{
    "success": false,
    "message": "Validation errors",
    "data": {
        "name": [
            "The name field is required."
        ]
    }
}
13
  • 1
    Please, do not upload images of code or errors. Commented Apr 12, 2024 at 7:34
  • 3
    Try to read the link in my comment. Commented Apr 12, 2024 at 7:37
  • 1
    Thanks. I'll replace with actual code instead Commented Apr 12, 2024 at 7:40
  • 1
    @tuna_tumi See here. It's a known issue with PHP, not even Laravel. You need to actually use POST as a method when you send the request, and add _method=PUT as a param (or in the form data!) Commented Apr 12, 2024 at 8:00
  • 3
    I don't think Laravel still has the problem described in the answer Lua linked to - that was six years ago. I use POST, PATCH, and PUT in Laravel without an issue. When you send data in the body and have validation rules defined in the request object, those validation rules are applied. If you wrote the validation to require the name why are you surprised the validation fails if you don't supply that attribute? If you want different validation rules for PUT or PATCH, use a different Request class. Commented Apr 12, 2024 at 9:08

2 Answers 2

0

the error message is clear, by using RoleRequest, you are validating the parameters entered in the request before it reaches the rest of the function. This is very useful to ensure that the fields that are required arrive.

You can go into RoleRequest and see that you have the field name as required. That means that the failure is due to not having sent that parameter in the request of your call.

Sign up to request clarification or add additional context in comments.

Comments

0

I've copied my comment to this answer so I can add more information.

I don't think Laravel still has the problem described in the answer Lua linked to - that was six years ago. I use POST, PATCH, and PUT in Laravel without an issue.

When you send data in the body and have validation rules defined in the request object, those validation rules are applied. If you wrote the validation to require the name why are you surprised the validation fails if you don't supply that attribute? If you want different validation rules for PUT or PATCH, use a different Request class.

However, I don't think you should be using PUT to update your resource. The semantic of a PUT action is to replace the resource with the data provided.

If the validation of a POST request is to require the name attribute, why would you not require it if you are replacing the resource?

Use PATCH when you only want a partial update and write a new RoleUpdateRequest class to validate fields only when they are present.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.