Creating default object from empty value "error in laravel" - laravel

Hi I'm working on update products form of e-commerce site but when I try to edit details then it shows error "Undefined variable: fileName" and error line is:
Product::where(['id'=>$id])->
update(['product_name'=>$data['product_name'],
'product_code'=>$data['product_ code'],
'product_color'=>$data['product_color'],
'description'=>$data['description'],
'price'=>$data['price'],'image'=>$fileName]);
return redirect()->back()->with('flash_message_success','Product
updated successfully!');
or when i try to update image only then its error is: "Creating default object from empty value", or error line is:
$product->image = $filename;
this is code of ProductsController:
public function editProduct(Request $request, $id=null){
if($request->isMethod('post')){
$data = $request->all();
//echo "<pre>"; print_r($data); die;
if($request->hasFile('image')){
$image_tmp = Input::file('image');
if($image_tmp->isValid()){
$extension = $image_tmp->getClientOriginalExtension();
$filename = rand(111,99999).'.'.$extension;
$large_image_path =
'images/backend_images/products/large/'.$filename;
$medium_image_path =
'images/backend_images/products/medium/'.$filename;
$small_image_path =
'images/backend_images/products/small/'.$filename;
// Resize Images
Image::make($image_tmp)->save($large_image_path);
Image::make($image_tmp)->resize(600,600)->save($medium_image_path);
Image::make($image_tmp)->resize(300,300)->save($small_image_path);
// Store image name in products table
$product->image = $filename;
}
}
if(empty($data['description'])){
$data['description'] = '';
}
Product::where(['id'=>$id])-
>update(['product_name'=>$data['product_name'],
'product_code'=>$data['product_code'],
'product_color'=>$data['product_color'],
'description'=>$data['description'],
'price'=>$data['price'],'image'=>$fileName]);
return redirect()->back()->with('flash_message_success','Product
updated successfully!');
}
//Get product details
$productDetails = Product::where(['id'=>$id])->first();
return view('admin.products.edit_product')-
>with(compact('productDetails'));
}

"Undefined variable: fileName"
There's a typo in your variable. Change $fileName to $filename.

i guess you need to do something like this
$product=new Product();
$product->image = $filename;
also where did you define $fileName in your code?

Related

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

How do grant rw access to public folder in laravel

I get this error after i migrated my project from windows to mac.
The "/private/var/folders/6w/zypn4xb120l6x6f1kjx9_nxw0000gn/T/phpwroBVT" file does not exist or is not readable.
here is my code
if($request->hasFile('image')){
$image = $request->file('image');
$image_name = $image->getClientOriginalName();
$destinationPath = public_path('/images/services');
$image->move($destinationPath, $image_name);
$summary = $request->summary;
$body = $request->body;
$title = $request->title;
$service = Service::create([
'title'=> $title,
'summary'=>$summary,
'body'=>$body,
'image'=> $image
]
);
if($service){
return redirect()->back()->with('success','services added');
}
}
the images goes into the public/images/services folder but i get the above error
I faced the similar error and I found out that i was accessing the global variable declared in the controller without "this" pointer reference. For example
class BlogController extends Controller
{
public $attachment_folder_name = "/blogs/";
}
You should access this variable with following syntax.
echo $this->attachment_folder_name
And not like this
echo $attachment_folder_name

Laravel 5 - Image Upload and Resize using Intervention Image Package

I want to upload a photo with some posts.
This is my controller
public function store(WisataRequest $request)
{
$input = $request->all();
if ($request->hasFile('gambar')) {
$gambar = $request->file('gambar');
$filename = time() . '.' . $gambar->getClientOriginalExtension();
if ($request->file('gambar')->isValid()) {
Image::make($gambar)->resize(300, 300)->save(public_path('/upload/gambar/'.$filename));
$input->gambar = $filename;
$input->save();
}
}
$wisata = Wisata::create($input);
Session::flash('flash_message', 'Berhasil Terkirim');
return redirect('admin_wisata');
}
But when it runs i found an error
Attempt to assign property of non-object
Change
$input->gambar = $filename;
$input->save();
To
$input['gambar']= $filename;
$input variable is not an object, it is an array. You can try accessing gambar in $input by doing $input['gambar']
You can put
$input['gambar']= $filename;
Instead of
$input->gambar = $filename;
$input->save();
OR
public function store(WisataRequest $request)
{
$wista = new Wista;
$wist->name = $request->name;
-----
$wista->save();
if ($request->hasFile('gambar')) {
$gambar = $request->file('gambar');
$filename = time() . '.' . $gambar->getClientOriginalExtension();
if ($request->file('gambar')->isValid()) {
Image::make($gambar)->resize(300, 300)->save(public_path('/upload/gambar/'.$filename));
$wista->gambar = $filename;
$wista->save();
}
}
Session::flash('flash_message', 'Berhasil Terkirim');
return redirect('admin_wisata');
}

I want to delete the stored image while update new image

I want to delete the stored image while update new image
public function update($id)
{
$users = AdminLogin::find($id);
if(Input::hasFile('image_file'))
{
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
return response()->json($users);
}
You can write this. This will solve your problem
public function update($id)
{
$users = AdminLogin::find($id);
if(Input::hasFile('image_file'))
{
$usersImage = public_path("uploads/images/{$users->image_file}"); // get previous image from folder
if (File::exists($usersImage)) { // unlink or remove previous image from folder
unlink($usersImage);
}
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
return response()->json($users);
}
This will delete the previous image and update the new image
Well, the answer is technically incorrect. What if the save operation fails, since you have deleted that image the current record will not have an image anymore.
So to overcome this problem you can adjust your code like:
if(Input::hasFile('image_file'))
{
$file = Input::file('image_file');
$name = time() . '-' . $file->getClientOriginalName();
$file = $file->move(('uploads/images'), $name);
$users->image_file= $name;
}
$users->save();
if(Input::hasFile('image_file'))
{
$usersImage = public_path("uploads/images/{$users->image_file}"); // get previous image from folder
if (File::exists($usersImage)) { // unlink or remove previous image from folder
unlink($usersImage);
}
}

image upload code is not working

i am new in laravel, i am facing a big problem and i can't solve this problem.
I am trying to upload image into my database and upload folder,but my code is not working,
Here is my controller code
public function store()
{
$rules=array(
'gallery_name'=>'required',
'image_title'=>'required',
'image'=>'required'
);
$file = Input::file('image');
$destinationPath = 'uploads/';
// If the uploads fail due to file system, you can try doing public_path().'/uploads'
$filename = str_random(32) . '.' . $file->getClientOriginalExtension();
//$filename = $file->getClientOriginalName();
//$extension =$file->getClientOriginalExtension();
//get all book information
$galleryInfo = Input::all();
//validate book information with the rules
$validation=Validator::make($galleryInfo,$rules);
$upload_success = Input::file('image')->move($destinationPath, $filename);
if($validation->passes())
{
//save new book information in the database
//and redirect to index page
if ($upload_success) {
//save in the Band table database
Gallery::create($galleryInfo);
return Redirect::route('galleries.index')
->withInput()
->withErrors($validation)
->with('message', 'Successfully created Gallery.');
}
else {
return Redirect::route('galleries.create')
->withInput()
->withErrors($validation)
->with('message', 'Image fields are incomplete.');
}
}
//show error message
return Redirect::route('galleries.create')
->withInput()
->withErrors($validation)
->with('message', 'Some fields are incomplete.');
}
please help me as soon.
Thank you.

Resources