Reputation: 419
I want to upload multiples images and store her URL into a database.
Here's my code:
<form method="POST" action="{{ route('validated', ['id' => $product->id]) }}) }}">
@csrf
<input type="hidden" name="ID" name="ID" value="{{ $product->id }}">
<div class="form-group row">
<label for="Image" class="col-sm-2 col-form-label">Image</label>
<img src="{{ $product->imageURL }}" />
</div>
<div class="form-group row">
<div class="col-sm-12">
<button type="submit" class="btn btn-lg btn-success">Valider les données</button> | <a href="#">Relancer le fournisseur</a>
</div>
</div>
</form>
and into my Controller function:
$imagePath = Storage::putFile('uploads/image/', $request->image, "imagename");
I have this error:
Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "Call to a member function hashName() on string"
Stacktrace: 0 Symfony\Component\Debug\Exception\FatalThrowableError in C:\laragon\www\MyProject\vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php:208
Upvotes: 0
Views: 2733
Reputation: 679
Do this if you want to write a reusable code and use it differently
for example we have Post and Image model in Image model add this code
public function post()
{
return $this->belongsTo(Post::class);
}
public static function named($name,$type)
{
$photo = new static;
$photo->saveAs($name,$type);
return $photo;
}
public function saveAs($name,$type)
{
$this->path = sprintf("%s/%s/%s-%s",'baseDir',$type,time(), $name);
$this->name = sprintf("%s-%s", time(), $name);
$this->type = $type;
}
public function moves(UploadedFile $file,$type)
{
$file->move($this->baseDir.'/'.$type, $this->name);
return $this;
}
and in a model that you want add an image for that like Post :
public function images()
{
return $this->hasMany(Image::class);
}
public function addImages(Image $image)
{
return $this->images()->save($image);
}
and in controllers :
public function addImages(ImageRequest $request,$id)
{
$image = $this->makeImage($request->file('file'),$request->type);
Post::where('id' ,'=', $id)->first()->addImage($image);
return back();
}
protected function makeImage(UploadedFile $file,$type)
{
return Image::named($file->getClientOriginalName(),$type)->moves($file,$type);
}
Upvotes: 1
Reputation: 3030
If you are using Laravel, you should be able to do the following:
$imagePath = $request->file('image')->store('uploads/image');
Or if you would like to specify the filename, you can do:
$imagePath = $request->file('image')->storeAs('uploads/image', 'myfilename.jpg');
UPDATED
I have noticed that in your HTML form you don't have a file upload input. If you don't have one, Laravel won't have a file to store.
Upvotes: 1
Reputation: 4153
First of all your form must has a enctype="multipart/form-data"
attribute if you want to upload image.
Based on Laravel documentation you can use something like this:
$path = $request->file('image')->store('image');
Upvotes: 2
Reputation: 1981
For multiple images upload, you can try this code:
$images = $request->file('images');
foreach ($images as $key => $image) {
if ($request->hasFile('images') && $request->file('images')[$key]->isValid()) {
$urlPath = $request->images[$key]->store('public/images');
$image = new Images();
$image->path = $urlPath;
$image->save();
} else {
return redirect()->back()->withInput()->with('errors', 'Invalid Image file found!');
}
}
Assuming that you have to added the enctype="multipart/form-data"
in your form and Request $request
in your function arguments and also storage link
in your public directory.
I hope it would helpful. Thanks
Upvotes: 1
Reputation: 41
Hi i think you need to use putFileAs method to customize the name for example:
//Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');
This method accepts either a Illuminate\Http\File or Illuminate\Http\UploadedFile instance and will automatically stream the file to your desired location:
Upvotes: 1