Reputation: 449
I am trying to update my laravel project by replacing image with a new one, but for some reason it is not working. When i make a new posts, images that i added show up, but when i update image it does not change. Image file also does not show up in laravel folder when i update my image post. I am using laravel 8.
PRODUCTSCONTROLLER.PHP
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
public function edit($id){
$product = Product::findOrFail($id);
return view('products.edit', ['product' => $product]);
}
public function update(Request $request, $id){
request()->validate([
'product_name' => ['required', 'min:2', 'max:50'],
'product_price' => 'required',
'product_desc' => ['required', 'min:2', 'max:300'],
'product_img' => 'image|nullable|max:1999'
]);
// Handle File Upload
if($request->hasFile('product_img')){
// Get filename with the extension
$filenameWithExt = $request->file('product_img')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('product_img')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('product_img')->storeAs('public/uploads', $fileNameToStore);
}
$product = Product::findOrFail($id);
$product->product_name = request('product_name');
$product->product_price = request('product_price');
$product->product_desc = request('product_desc');
if($request->hasFile('product_img')){
$product->product_img = $fileNameToStore;
}
$product->save();
return redirect('/products');
}
}
EDIT.BLADE.PHP
<div class="product-wrapper">
<div class="container">
<form method="POST" action="/products/{{$product->id}}">
@csrf
@method('PUT')
<div class="form-row">
<div class="col-md-6">
<label for="product-name">Toote nimi</label>
<input type="text" class="form-control {{ $errors->has('product_name') ? 'alert-danger' : '' }}" name="product_name" id="product-name" placeholder="" value="{{ $product->product_name }}" required>
</div>
</div>
<div class="form-row">
<div class="col-md-6">
<label for="product-desc">Toote hind</label>
<input type="text" class="form-control {{ $errors->has('product_price') ? 'alert-danger' : '' }}" name="product_price" id="product-price" placeholder="" value="{{ $product->product_price }}" required>
</div>
</div>
<div class="form-row">
<div class="col-md-6">
<input type="file" name="product_img">
</div>
</div>
<div class="form-row">
<div class="col-md-6">
<label for="product-desc">Toote kirjeldus</label>
<textarea class="form-control {{ $errors->has('product_desc') ? 'alert-danger' : '' }}" name="product_desc" id="product-desc" required>{{ $product->product_desc }}</textarea>
</div>
</div>
<input type="submit" class="btn btn-primary" value="submit">
</form>
</div>
</div>
WEB.PHP
Route::get('/products/edit/{product}', [\App\Http\Controllers\ProductsController::class,'edit'])->middleware('auth');
Route::put('/products/{product}', [\App\Http\Controllers\ProductsController::class,'update'])->middleware('auth');
Upvotes: 0
Views: 606
Reputation: 4783
You have to add the enctype
attribute to your update form
with the value of multipart/form-data
to allow files upload (images in your case), you'll have something like <form method="POST" action="/products/{{$product->id}}" enctype="multipart/form-data">...</form>
Upvotes: 1