Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fatima1's avatar

unlink files fron public directory

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);
    });
}

}

0 likes
16 replies
RamjithAp's avatar

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;
dinhquochan's avatar

Use model deleting events, Files:


use Illuminate\Support\Facades\File;

...

public static function boot()
{
    parent::boot();

    static::deleting(function ($model) {
        File::delete($model->pathFile);
    });
}
dinhquochan's avatar

If you want delete Job but not delete Files, Just set null foreign column and set null onDelete. Example:

$table->unsignedInteger('job_request_id')->nullable();
$table->foreign('job_request_id')
                ->references('id')
                ->on('job_requests')
                ->onDelete('set null');
fatima1's avatar

@dinhquochan i want to delete job then delete jobRequests and files relate it to.

i have done these but the file still in the public

dinhquochan's avatar

Hey, you must hard remove when model event deleting was fire. 2 ways:

use facade:

File::delete($path);

or, PHP function:

@unlink($path);
Snapey's avatar

You need to show your code, you could be using the wrong file path - we cannot tell.

Suggesting different ways to trigger the delete is pointless if you get the filepath wrong.

1 like
harshvardhan's avatar

You have to show your code first may be your getting in some wrong way

Snapey's avatar

does your 'deleting' event handler get called?

It will only fire when you delete File model with Eloquent - not if the row is being deleted from the table because of a delete cascade

Snapey's avatar

how are you deleting the File model?

fatima1's avatar

@Snapey

i use foreign key in each table like that:

$table->foreign('jobReq_id')->references('id')->on('job_reqs')->onDelete('cascade');

so its deleted from database

Snapey's avatar

So you cannot do anything in Laravel to delete the file automatically because this delete cascade is performed in mysql.

This is why I very rarely use this method because it prevents performing other Laravel activities.

fatima1's avatar

@Snapey so its better to remove this method to delete the file .

if i remove onDelete('cascade') can i use this way in the file model

public static function boot() { parent::boot();

static::deleting(function ($model) {
    File::delete($model->pathFile);
});

}

Snapey's avatar

Somewhere in your code, you are deleting a model. That model needs the code to watch for deleting of the model and its job is to call delete on any related models.

The delete function on the related model needs to delete the file from disk

Please or to participate in this conversation.