Use the unlink function of php, just pass the exact path to your file to unlink function :
unlink($file_path);
Do not forget to create a complete path of your file if it is not stored in the DB. e.g
$file_path = app_path().'/path-to-file;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i have three tables Job, JobRequest, Files.
job is one to many with jobRequest,
jobRequest is one to many with files,
i want to unlink files when the job delete from Files model
i have found several ways but it doesn't work for me
UPDATE:
class Job extends Model { protected $fillable = ['title','arabic_title','arabic_description','description','visible','no_req','order'];
protected $dates = ['deleted_at'];
protected $table = 'jobs';
protected $primaryKey = 'id';
public function jobreqs()
{
return $this->hasMany('App\JobReq','job_id');
}
}
class JobReq extends Model { protected $fillable = ['name','job_id','email', 'message','review','order','title'];
protected $dates = ['deleted_at'];
public function scopeReviewed($query)
{
return $query->where('reviewed',1);
}
public function job()
{
return $this->belongsTo('App\Job','job_id');
}
public function files()
{
return $this->hasMany('App\File','jobReq_id');
}
}
class File extends Model { protected $fillable = ['filename','jobReq_id'];
protected $dates = ['deleted_at'];
// protected $casts = [ 'filename' => 'array'];
public function jobReq()
{
return $this->belongsTo('App\JobReq','jobReq_id');
}
public static function boot()
{
parent::boot();
static::deleting(function ($file) {
File::delete(public_path('/files/').$file->filename);
});
}
}
Please or to participate in this conversation.