how to fix function getClientOriginalExtension() on null on laravel - laravel

check out the code that i am getting error on "Call to a member function getClientOriginalExtension() on bool"
i used image intervention properties on laravel to upload image on product table . while usng getClientOriginalExtension() i got error...
public function product_store(Request $request)
{
$image = $request->hasfile('product_image');
$img = $image->getClientOriginalExtension();
$location = public_path('images/products/' .$img);
Image::make($imge)->save($location);
$product_image = new productImage;
$product_image->product_id = 1;
$product_image->image = $img;
$product_image->save();
return redirect() -> route('admin.product.create');
}
Call to a member function getClientOriginalExtension() on bool

hasFile returning boolean.Try this -
$image = $request->file('product_image');

You can use hasFile() to check if there is a file or not!
if ($request->hasFile('product_image')) {
$image = $request->file('product_image');
}
then get the extension using :
$imageExt = $image->extension();

Related

Call to a member function getClientOriginalName() on string laravel

$path = "public/images/";
$Image->banner_image_1 = (is_null($input['banner_image_1']))?($Image->banner_image_1) : ($input['banner_image_1']); $imageName = $Image->banner_image_1->getClientOriginalName();
$file = $Image->banner_image_1;
$file->storeAs($path, $imageName, 's3');
$url = Storage::disk('s3')->url($path.$imageName);
$Image->banner_image_1 = $imageName;
The error Call to a member function getClientOriginalName() on string is occuring while update. How to solve this error?

Laravel 8 how to use setter method to upload an image

I was using the old Laravel setter method in the model to upload an icon image when creating a new Category
class Category extends Model
{
public function setIconAttribute($value)
{
$fileName = 'Icon_'. $this->name .'_'.rand(11111,99999).'.'.$value->getClientOriginalExtension();
$destinationPath = public_path('images/Icon');
$value->move($destinationPath, $fileName); // uploading file to given path
$this->attributes['icon'] = $fileName;
}
}
and it was working fine ,
now I am upgrading to the new laravel 8.77 setter
protected function icon(): Attribute
{
return Attribute::make (
get: fn ($value) => asset('images/Icon/'.$value),
set: function ($value) {
$fileName = 'Icon_'.rand(11111,99999).'.'.$value->getClientOriginalExtension(); // renameing image
$destinationPath = public_path('images/Icon');
$value->move($destinationPath, $fileName); // uploading file to given path
return $fileName;
}
);
}
and it gets me an error
The file "Finishe_30574.png" was not uploaded due to an unknown error.
so first how to fix this error
and how to add the attribute name ($this->name) in the file name - in the new Laravel-8 setter like the old way
$fileName = 'Icon_'. $this->name .'_'.rand(11111,99999).'.'.$value->getClientOriginalExtension();
because adding ($this->name) in the new setter way getts an error

Image resizing not working with Intervention image Package

The photo is saved, but without resizing, the original photo is actually saved
protected function uploadImage($images = '') {
$path = 'upload/images';
if (request()->hasFile('image') && $files = request()->file('image'))
$images = Image::make($files->store($path, 'public_files'))->resize(320, 240);
return $images;
}
public function store(CreateGalleryRequest $request, Job $job) {
$image = $this->uploadImage();
if ($request->hasFile('image')) {
$data['image'] = $image . $image->dirname . '/' . $image->basename;
} else
$data['image'] = null;
$job->gallery()->create($data);
return redirect(route('jobs.gallery.index' , ['job' => $job->id]));
}
You need to call save() function to save image
...
$images = Image::make($files->store($path, 'public_files'))->resize(320, 240)->save('image path');
...
wrote:
$images = Image::make($files->store($path, 'public_files'))->resize(320, 240)->save($files->store($path, 'public_files'));
error:
Illuminate\Database\QueryException
actually there is an error in the following code:
$job->gallery()->create($data);

I am trying to send multiple title and multiple files into database using laravel but i am getting error

I am trying to send multiple title and multiple files into database using laravel but i am getting error please help me how can i resolve this issue ? thanks.
getting error
Cannot use object of type Illuminate\Http\UploadedFile as array
CONTROLLER
public function store(Request $request)
{
foreach ($request->title as $key => $value) {
$audiodetail = new AudioDetail;
$extension = Str::random(40) . $request->file('audio_file_upload')->getClientOriginalExtension();
$audiodetail->audio_file = Storage::disk('audiofile')->putFileAs('', $request->audio_file_upload[$key], $extension);
$audiodetail->title = $value;
$audiodetail->audio_id = $event->id;
$audiodetail->save();
}
return redirect()->route('events');
}
title and audio_file_upload both are different array. So you need to combine both :
public function store(Request $request)
{
$data = array_combine($request->title, $request->audio_file_upload);
foreach ($data as $value) {
$audiodetail = new AudioDetail;
$extension = Str::random(40) . $value['audio_file_upload']->getClientOriginalExtension();
$audiodetail->audio_file = Storage::disk('audiofile')->putFileAs('', $value['audio_file_upload'], '.' . $extension);
$audiodetail->title = $value['title'];
$audiodetail->audio_id = $event->id;
$audiodetail->save();
}
return redirect()->route('events');
}

Call to a member function getClientOriginalName() on a non-object - Laravel 4.2

I am trying to upload an image within my form data
here is the form header
{{ Form::open(array('url'=>'doAddProject', 'file'=>'true', 'method'=>'PUT')) }}
and here is the controller
public function store()
{
$project = new Projects();
$file = Input::file('pImage');
$destination_path = 'images/projects/';
$filename = str_random(6) . '_' . $file->getClientOriginalName();
$file->move($destination_path, $filename);
$project->main_image = Input::file($filename);
$project->pro_title = Input::get('pName');
$project->pro_map = Input::get('pMap');
$project->pro_description = Input::get('pDetails');
$project->pro_serves = implode(",", array_filter(Input::get('pro_serves')));
$project->pro_activity = Input::get('activity');
$project->save();
return Redirect::to('admin/view-project')->with('message', 'Project add successfully');
}
Got Error :
Call to a member function getClientOriginalName() on a non-object
The Line
$project->main_image = Input::file($filename);
Change it to
$project->main_image = $filename;

Resources