Method save doesnot exist. Laravel - php

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();
}

Related

Resize image and store in Storage folder

Why does not this work?
I want to change the size and save it in the storage folder.
public function store(Request $request)
{
$image = $request->file('image') ?? null;
if ($request->hasFile('image'))
{
$file = $request->file('image');
$name = time();
$extension = $file->getClientOriginalExtension();
$fileName = $name . '.' . $extension;
$imageResize = Image::make($file)->resize(600, 300)->save('images/blogs/'.$fileName);
$image = $file->storeAs('',$imageResize, 'public');
}
Blog::query()->create([
'image' => $image,
]);
return redirect()->route('admin.blogs.index');
}
I see this error
Images must be saved in this way
Please try this and if you want add time to the name of your image simply add it to the input image name.
About permission if you are an a production server please set storage directory permission to the 755 but if you are on a local windows machine check if your storage directory has not read-only attribute.
public function store(Request $request)
{
$image = $request->file('image') ?? null;
if ($request->hasFile('image'))
{
$image = $request->file('image');
$input['imagename'] = $request->file('image')->getClientOriginalName();
$destinationPath = public_path('storage/images/blogs/resized');
$img = Image::make($image->path());
$img->resize(600, 300, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath.'/'.$input['imagename']);
$destinationPath = public_path('storage/images/blogs/fullSize');
$image->move($destinationPath, $input['imagename']);
}
Blog::query()->create([
'image' => $image,
]);
return redirect()->route('admin.blogs.index')->withSuccess('Image successfully uploaded!');
}

delete image and thumbnail image in laravel

i try to delete some image file in laravel project. i follow tutorial on website, and its work, but tutorial only deleted image based name in column image. the thumbnail on folder image didnt deleted. how to delete image also the thumbnail image? pls help.
this is the store function.
if ($request->hasFile('image'))
{
$file = $request->file('image');
$image = $file->getClientOriginalName();
$destination = public_path() . '/galeri/';
$successUploaded = $request->file('image')->move($destination, $file->getClientOriginalName());
if($successUploaded)
{
$extension = $file->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $image);
Image::make($destination . '/' . $image)
->resize(250, 170)
->save($destination . '/' . $thumbnail);
}
$photo->image = $image;
}
$photo->save();
and this the destroy function
$photo = Photo::where('id', $id)->first();
File::delete('galeri/'.$photo->image);
if($photo){
$photo->delete();
}
Delete a single file
File::delete($filename);
Delete multiple files
File::delete($file1, $file2, $file3);
Delete an array of files
$files = array($file1, $file2);
File::delete($files);

Failed to rename image and upload using storeAs in Laravel

I created a form to store article with an image , and generate a resized version as thumbnail from it.
I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error
This is the image upload handler function in my controller :
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug;
$successUploaded = $image->storeAs('img/articles-images', $fileName);
if($successUploaded) {
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make('img/articles-images' . '/' . $fileName)
->resize($width, $height)
->save('img/articles-images' . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
storeAs() method, which receives the path, the file name, and the (optional) disk as its arguments :
$successUploaded = $request->file('image')->storeAs(
'images', $fileName
);
I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug.'.' .$image->getClientOriginalExtension();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
// //

Upload image with different name but keep file extension

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;
}

how to save multiple images in table using laravel php

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);
}

Categories