Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

you should do

File::delete('/path/to/image/file');
Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

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();
	}
Last updated 2 years ago.
0

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();
}
Last updated 2 years ago.
0

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()?

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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);
Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.