What is the best way to handle automatic deletion of files if deleting the record?
Example very minimal for this example like a photos table with some basic fields id, location(path to the upload file).
When I delete id = 7, I need to delete the file from "location" value also. So there are no straggling uploaded files that belong to no record.
I was thinking of using in the Photo.php model using the deleted hook.
public static function boot()
{
self::deleted(function($model){
// record has been deleted so its now safe to delete the files related to this record
});
}
@movepixels - hooking into eloquent events would be how I would approach this problem.
Personal preference is I would put the logic within a moddl observer but only because I usually have other logic for creating/updating and don't want to bloat my models