0

I am trying to use make a put request using custom request validation. Here is my code:

api.php

Route::put('/update-pdf-resource/{resource}', 'ManagementController@updatePdfResource');

ManagementController.php

public function updatePdfResource(Resources $resource, UpdatePdfResourceRequest $request)
{

   $resource->type = $request->type;
   $resource->title = $request->title;
   $resource->file = $request->file('file')->store('files', 'public')l
   $resource->save();
   return response(['message' => 'Resource updated successfully'], 201);
}

UpdatePdfResourceRequest.php

public function rules()
{
   return [
      'type' => 'required',
      'title' => 'required',
      'file' => 'required | mimes:pdf'
   ];
 }

Now whenever I try to update a entry it sends me following error enter image description here

The Request header is

enter image description here

It's hitting the correct route with correct data, but throwing validation error. How to solve it ?

Correction:

I just returned the $request beginning of the updatePdfResource method & it returns an empty array ! but in header it's showing the proper payload as above !

3
  • echo your form field data and check is it empty or not Commented Sep 29, 2021 at 6:10
  • please see the edited question Commented Sep 29, 2021 at 6:24
  • Are you sure you are sending your data in a proper format? If you're using JSON, you need the json header, if you're using multipart, add the multipart header. Commented Sep 29, 2021 at 6:32

1 Answer 1

0

Requests should always come first

change it

public function updatePdfResource(Resources $resource, UpdatePdfResourceRequest $request)

to this

public function updatePdfResource(UpdatePdfResourceRequest $request, Resources $resource)

and you have a syntax error in your code "l" instead of ";"

->store('files', 'public')l
Sign up to request clarification or add additional context in comments.

1 Comment

Corrected. But not working. Please see the edited question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.