Support the ongoing development of Laravel.io →
posted 9 years ago
Input
Last updated 1 year ago.
0

The error is in postEdit function?
Honestly I do not know what File::update should do, I did not find any documentation for this.

Can you please give some info on File::update function?

Last updated 1 year ago.
0

I thought update was a global function in the intervention class but realized it was not. My mistake, but I do not know how I should update the image. Is the logic to overwrite the image or grab the file that exist and use some update method in intervention?

I know the error is at

File::update('public/'.$product->image);

I just do not know how to solve this.

Last updated 1 year ago.
0

Well,

I suppose that when you edit the Product you get the model by

 $product = Product::find(Input::get('id'));

That part you have OK.

Then I would expect that a product has a property "image" that you want to change for another "image".
You then need to send the image by form to your controller again, read it and upload it again, and save the product "image" property from the model in postEdit function exactly the way you do in postCreate.

 $image = Input::file('image');
 $filename  = time() . '.' . $image->getClientOriginalExtension();
 $path = public_path('img/products/' . $filename);
 Image::make($image->getRealPath())->resize(468, 249)->save($path);
 $product->image = 'img/products/'.$filename;
 $product->save();

You can create some function for the code above, so you can reuse it.
Or am I missing something here? :)

Last updated 1 year ago.
0

I initially tried this at the beginning but got this error

Call to a member function getClientOriginalExtension() on a non-object
Last updated 1 year ago.
0

Paste the code from view for editing the product please

Last updated 1 year ago.
0

Here is my product CRUD view http://laravel.io/bin/JzE5W Here is the product controller http://laravel.io/bin/Bj85N Here is the product model http://laravel.io/bin/214RM

Last updated 1 year ago.
0

You are missing

'files'=>true

in

 {{ Form::open(array('url'=>'admin/products/edit', 'class'=>'form-inline'))}}
Last updated 1 year ago.
0

That helped some. The error does not show, but when the form is submitted, it flashes updated but with an empty image link.

Last updated 1 year ago.
0

Try moving

 $product->update(Input::all());

right after the

 if ($product) {

in postEdit function in controller

Last updated 1 year ago.
0

That was the problem and it is solved! Thank you so much. It was a simple error. But I do not understand why the

 $product->update(Input::all());

had to be outside of the conditional, where as in the postDestroy function the delete method was called in the conditional?

Here is the code to help others

public function postEdit() {
    	$product = Product::find(Input::get('id'));
 
 
		if ($product) {
			$image = Input::file('image');
            $filename  = time() . '.' . $image->getClientOriginalExtension();
            $path = public_path('img/products/' . $filename);
            Image::make($image->getRealPath())->resize(468, 249)->save($path);
            $product->image = 'img/products/'.$filename;
            $product->save();
			
			return Redirect::to('admin/products/index')
			->with('message', 'Product Updated');
		}
 		$product->update(Input::all());
		return Redirect::to('admin/products/index')
			->with('message', 'Something went wrong, please try again');
	}
Last updated 1 year ago.
0

No no no :) Actually the

  $product->update(Input::all());

updates all attributes that are present in the input. But the Input::get('image') in this case is empty (Input::file('image') is not). That is why it set the blank value. And also that is why, the image property must be set otherway.

You can leave it inside the conditional, but the reasoning is that it must be called before the

 $product->image = 'img/products/'.$filename;

This line will set the image property again with correct value.

BETTER solution however is this

 $product = Product::find(Input::get('id'));


	if ($product) {
		$image = Input::file('image');
        $filename  = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('img/products/' . $filename);
        Image::make($image->getRealPath())->resize(468, 249)->save($path);
        $product->image = 'img/products/'.$filename;
        $product->save();
		$product->update(Input::except('image'));
		return Redirect::to('admin/products/index')
		->with('message', 'Product Updated');
	}

	return Redirect::to('admin/products/index')
		->with('message', 'Something went wrong, please try again');
}

The difference is in

 $product->update(Input::except('image'));

This will set all attributes in Input except "image"

Last updated 1 year ago.
0

The word "except" is throwing me off. Why would you want to update all the products (excluding)except the image. Am I reading this wrong?

Last updated 1 year ago.
0

You want to update all attributes excluding "image" because you set the "image" property alone with this:

$product = Product::find(Input::get('id'));
...
$product->image = 'img/products/'.$filename;
$product->save();
Last updated 1 year ago.
0

forgot to thank you:)

Last updated 1 year ago.
0

Thank you both, because I got a lot of helpful information from this post, thank you again :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

swgj19 swgj19 Joined 11 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.