Hi please help me am new to laravel
I want to store multiple images in table... With this code am unable to save.
Help me for this..
Here in my View
{{Form::open(array('url'=>'businessdirectory/business', 'files'=>true))}}
{{Form::label('image','Upload Image')}}
<div class="form-group">{{Form::file('image[]',array('multiple'=>true))}}
</div>
{{Form::close()}}
In my Controller
if(Input::file('image'))
{
$image = Input::file('image');
foreach($image as $img) {
$destination = 'images';
$filename = $img->getClientOriginalName();
$path = 'images/'.$filename;
$uploadSuccess = $img->move($destination,$filename);
}
}
else
{
$path='images/default.JPG';
}
$business = new Business();
$business->image = $path;
It's not advisable to store images on database. Just save the path of the images instead.
If you really need to store image on db. Make sure you set the column to blob as it need more space. Then, get the image content and type, then save it.
<?php
$image = fopen($image_path, 'rb');
// or
$image = file_get_contents($image_path);
$business = new Business;
$business->image = $image;
$business->imageType = "image/gif"; //
$business->save();
// ...
You need to put below code inside the foreach loop. In every loop you need to insert image path inside a database
$business = new Business();
$business->image = $path;
This is a working code in laravel 5.2.
$files = $request->file('file');
if($request->hasfile('file')){
$destinationPath = 'uploads';
foreach($files as $file){
$image = new Image;
$filename = $file->getClientOriginalName();
$image->name = $filename;
$image->save();
$file->move($destinationPath,$filename);
}
Related
this is still related to my post in here How to remove and unlink multiple images in json format
i can store and delete all multiple images. but i have still problem when update and remove old image. my code still not remove the oldimage when update the new image.
my update controller, i use handlerequest method to store images, where oldImage is variable in database that will be compared with new images. if not same then remove old images.
public function update(Requests\UpdatePostRequest $request, $id)
{
$post = Post::findOrFail($id);
$oldImage = $post->image;
$data = $this->handleRequest($request);
$post->update($data);
if($oldImage != $post->image)
{
foreach (json_decode($post->image, true) as $oldImage) {
$this->removeImage($oldImage);
}
}
if($request->submitbutton =='Publish')
{
Alert::success('Your post was updated successfully')->persistent('Close');
return redirect(route('admins-blogpost.index'));
}
if($request->submitbutton =='Save Draft')
{
Alert::success('Your draft was updated successfully')->persistent('Close');
return redirect(route('admins-blogpost.index'));
}
}
my handlerequest method to store multiple image, this method is working using store method. so this problem not here.
private function handleRequest($request)
{
$data = $request->all();
if($request->hasFile('image'))
{
$images = $request->file('image');
foreach($images as $key=>$image){
$fileName = $image->getClientOriginalName();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
if($successUploaded)
{
$width = config('cms_blog.image.thumbnail.width');
$height = config('cms_blog.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make($destination . '/' . $fileName)
->resize($width,$height)
->save($destination . '/' . $thumbnail);
$datax[] = $fileName;
}
$data['image'] = json_encode($datax);
}
}
return $data;
}
and delete method
public function removeImage($image)
{
if( ! empty($image))
{
$imagePath = $this->uploadPath . '/' . $image;
$ext = substr(strrchr($image, '.'), 1);
$thumbnail = str_replace(".{$ext}", "_thumb.{$ext}", $image);
$thumbnailPath = $this->uploadPath . '/' . $thumbnail;
if(file_exists($imagePath) ) unlink($imagePath);
if(file_exists($thumbnailPath) ) unlink($thumbnailPath);
}
}
how to fix my problem so i can remove old images when their not same with new image?
In the for loop where you delete the images, I believe you wanted to iterate over $oldImage, not over $post->image:
foreach (json_decode($oldImage, true) as $item) {
$this->removeImage($item);
}
Storing multiple images name on a single column, which is not a good idea. I suggested you create another table post_images or something else.
And create hasMany relation from posts table with post_images table. Then you have lot's of flexibility here. Also, you can create a Polymorphic Relationship to use the same image model with multiple Models.
When i save image with my form i change the file name. Everything works nice except file extension. When i change file name the extension is missing and file is saved without it. Please help me to solve my problem i want to store also image extension.
This is my code where i update image name.
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_');
$store = $request->file('image')->storeAs(
$imgPath, $imgName
);
$post->image = $imgName;
}
Try this:
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_') . '.' . $image->extension();
$store = $image->storeAs($imgPath, $imgName);
$post->image = $imgName;
}
I am trying to insert in image table but following syntax is showing method save does not exit:BadMethodCallException in Macroable.php line 74:.
Controller:
foreach ($request->file('image') as $i)
{
$image = new image();
$image = $i;
$input['imagename'] = $request->vname.'_'.$user->id.'_'.str_random(2).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/images');
//move image to folder
$image->move($destinationPath, $input['imagename']);
$image->title=$input['imagename'];
$image->filepath=$destinationPath;
$image->Vehicle_id=$vehicles->id;
$image->save();
}
Can anyone tell me what am i doing wrong?
This will do the job.
You replaced your model's object with file here $image = $i; therefore no save method is availble for $image.
foreach ($request->file('image') as $file)
{
$image = new image();
$input['imagename'] = $request->vname.'_'.$user->id.'_'.str_random(2).'.'.$file->getClientOriginalExtension();
$destinationPath = public_path('/images');
//move image to folder
$file->move($destinationPath, $input['imagename']);
$image->title=$input['imagename'];
$image->filepath=$destinationPath;
$image->Vehicle_id=$vehicles->id;
$image->save();
}
Hi please help me am new to laravel I want to store multiple image paths in
table... With this code am unable to save.
Help me for this..
Here in my View
{{Form::open(array('url'=>'businessdirectory/business', 'files'=>true))}}
{{Form::label('image','Upload Image')}}
<div class="form-group">{{Form::file('image[]',array('multiple'=>true))}}
</div>
{{Form::close()}}
In my Controller
if(Input::file('image'))
{
$image = Input::file('image');
foreach($image as $img) {
$destination = 'images';
$filename = $img->getClientOriginalName();
$path = 'images/'.$filename;
$uploadSuccess = $img->move($destination,$filename);
}
}
else
{
$path='images/default.JPG';
}
$business = new Business();
$business->image = $path;
$business->save();
I am trying to upload an image using Laravel it is working to the point that the images are being successfully placed into the destination folder but when I try to view them within my IDE I am told they are corrupt and that netbeans cannot open them.
I am wondering if my upload process is causing this?
This is my code:
public function handleCreate(){
$book = new Book;
$book->title = Input::get('title');
$book->desc = Input::get('desc');
//Img uploading...
$destinationPath = '';
$filename = '';
if(Input::hasFile('cover')){
$file = Input::file('cover');
$destinationPath = public_path().'/img/';
$filename = str_random(6) . "_" . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
}
$book->cover = $filename;
$book->save();
return Redirect::route('books')->with('msg', 'test');
}
Any help is much appreciated. I know the image is not corrupt before uploading as I can view on my desktop. The issue is only after uploading it.