Home > Software design >  update image in laravel 8 - API
update image in laravel 8 - API

Time:10-28

I tried update my image, but it isn´t update.

  1. I have the method upload , it works in a store method
    private function upload($image)
    {
        $path_info = pathinfo($image->getClientOriginalName());`
        $post_path = 'images/post';
    
        $rename = uniqid() . '.' . $path_info['extension'];
        $image->move(public_path() . "/$post_path", $rename);
        return "$post_path/$rename";      
    }
  1. I tried update the new image, but the message update successfully apears but not update
  public function update(Request $request, Car $car)
  {
      if (!empty($request->file('image_url'))) {
          $url_image = $this->upload($request->file('image_url'));
          $car->image_url = $url_image;
      }

      $res = $car->save();

      if ($res) {
          return response()->json(['message' => 'Car update succesfully']);
      }

      return response()->json(['message' => 'Error to update car'], 500);
  }
  1. La actualización es correcta pero en la BDD no actualiza Imagen de actualización con POSTMAN

CodePudding user response:

Try changing the method in postman to POST and add this query string parameter to the URL: its name is _method and the value is PUT. Your API consumers will call the endpoint in this way but you will keep the route definition with the PUT verb. Read more about it here

CodePudding user response:

The request PUT doesnt have a body like the method POST as of the RFCs (Something with the Content-Length, dont remember correctly).

Even if your API can handle PUT methods with body, your postman will try to send the file in the query field (URL) and that will mostly break the file. You can check the URL called by postman in the request Headers.

Laravel has a workaround for this, you send the parameter _method=PUT in a POST request and the router will handle it as a PUT request (if you declared it so).

  • Related