Exception or error:
I’m using Laravel to get the request file from postman, however it seems Laravel cannot identify the file I posted. Following is my code:
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductImageController
{
public function update(Request $request, $id)
{
$path = $request->file('file')->store('images');
$product = Product::find($id);
$product->img_url = $path;
return $product->save() ?
response(["error_code" => 0, $product]) :
response(["error_code" => 1]);
}
}
How to solve:
add this to your method and check if there is any errors and check if form-data is selected in postman, Be careful with explicit Content-Type header. Better – do not set it’s value, the Postman is smart enough to fill this header for you. BUT, if you want to set the Content-Type: multipart/form-data – do not forget about boudary field.:
public function update(Request $request, $id)
{
$this->validate($request, [
'file' => 'required|file'
]);
...
Answer:
I think you can try this:
if ($request->hasFile('file')) {
$product->img_url = $request->file('file')->store('images');
}
AND more info please follow this link
Hope this work for you !!!
Answer:
try to:
-Change Put to Post in postman also in your ROUTE
-add this validator to your controller :
$validator = Validator::make($request->all(),[
'file' => 'file|required'
]);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 401);
}