you should do
File::delete('/path/to/image/file');
How do you keep your images in the page? Do you have a field/table in database or is it just an img tag in page content?
I am storing image path in a database column called "image" and moving images to upload directory:
public function saveCreate($input) {
$page = new Page;
$page->title = $input['title'];
// saving image in DB
if ($image = $input['image']) {
$filename = $image->getClientOriginalName();
$destination = 'uploads/';
$image->move($destination, $filename);
$page->image = $destination . $filename;
}
$page->body = strip_tags($input['body'], '<p><br>');
$page->slug = $input['slug'];
$page->save();
}
You can override the delete() function in your model, so it deletes the files on a delete.
public function delete(){
if($this->attributes['image']){
$file = $this->attributes['image'];
if(File::isFile($file)){
\File::delete($file);
}
}
parent::delete();
}
barryvdh said:
You can override the delete() function in your model, so it deletes the files on a delete.
public function delete(){ if($this->attributes['image']){ $file = $this->attributes['image']; if(File::isFile($file)){ \File::delete($file); } } parent::delete(); }
Thanks for your answer. But, is it possible to do that in a class deletePage()?
heihachi88 said:
Thanks for your answer. But, is it possible to do that in a class deletePage()?
Just delete image with File::delete('uploads/'.$this->page->image); before deleting page.
barryvdh said:
You can override the delete() function in your model, so it deletes the files on a delete.
public function delete(){ if($this->attributes['image']){ $file = $this->attributes['image']; if(File::isFile($file)){ \File::delete($file); } } parent::delete(); }
Can you explain please, why are you using backlash before:
\File::delete($file);
heihachi88 said:
barryvdh said:
You can override the delete() function in your model, so it deletes the files on a delete.
public function delete(){ if($this->attributes['image']){ $file = $this->attributes['image']; if(File::isFile($file)){ \File::delete($file); } } parent::delete(); }
Can you explain please, why are you using backlash before:
\File::delete($file);
For namespace :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community