1

I have created two databases one is parent and another is child. Parent table contains title and description column and child table contains the image column for the parent row. Now I want to delete a row from parent table and delete the image from folder it is stored to.

I have done:-

Code to delete from databases, Code to delete from child table and also remove image from folder the image is saved into, Code to delete rows from parent table.

I could not do:-

Code to delete from folder the image is saved into while deleting parent row.

My controllers are like this[

ProjectControllers controls my parent table ProjectImageControllers controls my child table

//This is my ProjectControllers.php 

public function destroy(Project $project)
    {
        $projectImage = Project::find(request('id'));
        $deleted = $projectImage->delete();
        if($deleted){
            File::delete(public_path('/images/projects/').$projectImage->image);
        }

        $projectList = Project::all();
        return view('admin.project.adminProject', ['success'=> true, 'projectList' => $projectList ]);
    }

]

Also the models relation are as follows[

this is Project.php model


class Project extends Model
{
    public function projectImages(){

        return $this->hasMany(ProjectImages::class);
    }
}


This is ProjectImages.php model


class ProjectImages extends Model
{
    protected $fillable = [
        'image', 'project_id'
    ];
    public function project(){
        return $this->belongsTo(Project::class, 'project_id');
    }
}

]

3
  • Maybe it's a permission problem try to use unlink() instead of File::delete() and dd() the result so you can see what is wrong. dd(unlink(public_path('/images/projects/').$projectImage->image)); Commented Sep 6, 2020 at 8:42
  • Have you tried to unlink image before you delete model entity? Commented Sep 6, 2020 at 14:39
  • No Its not working. Maybe we need to use loop to delete from file one by one. If not found in database then delete from file too.. or maybe any thing else.. I'm very new to coding could you help me with the loop concept? Commented Sep 6, 2020 at 14:43

1 Answer 1

1

Please get the rows of the child table first and then delete the images in the folder one by one as follows:

public function destroy(Project $project)
{
   //first find the Parent object with the help of the id parameter.         
     $project = Project::find(request('id'));

   //find the children list of the parent
     $projectImages = ProjectImages::where('project_id','=',request('id'));

    //delete the images stored in your filesystem one by one 
     foreach($projectImages as $projectImage){

          File::delete(public_path('/images/projects/').$projectImage->image);

     }
    
    //Now,finally delete the parent table data. 
     $project->delete();
    

    $projectList = Project::all();
    
    return view('admin.project.adminProject', ['success'=> true, 'projectList' => $projectList ]);
}

 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.