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?
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.
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? :)
I initially tried this at the beginning but got this error
Call to a member function getClientOriginalExtension() on a non-object
Paste the code from view for editing the product please
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
You are missing
'files'=>true
in
{{ Form::open(array('url'=>'admin/products/edit', 'class'=>'form-inline'))}}
That helped some. The error does not show, but when the form is submitted, it flashes updated but with an empty image link.
Try moving
$product->update(Input::all());
right after the
if ($product) {
in postEdit function in controller
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');
}
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"
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?
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();
Thank you both, because I got a lot of helpful information from this post, thank you again :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community