-2

this function delete Brand but photo still save in folder inside public/upload/brands/

i want when make delete it remove photo from folder also.

public function deleteBrand($request){

$deletelogo = $this->brandyModel::where('id',$request->id)->first();

$logo = $deletelogo->logo;
if (is_file($logo)){
unlink(public_path('upload/brands'.$logo));
}
$deletelogo->delete();
$notificat = array(
    'message' => 'Brand Deleted Successfully',
    'alert-type' => 'success',
);
return redirect()->back()->with($notificat);

}

Я Диша
  • 1
  • 1
  • 5

2 Answers2

0

I think it just typo, let me refactor your code

public function deleteBrand($request){

$deletelogo = $this->brandyModel::where('id',$request->id)->first();

$logo = $deletelogo->logo;
$url = public_path('view/upload/brands/'.$logo); // Take a look at this Url, you might wanna change it
if (file_exists($url)){
     unlink($url);
}
$deletelogo->delete();
$notificat = array(
    'message' => 'Brand Deleted Successfully',
    'alert-type' => 'success',
);
return redirect()->back()->with($notificat);
Wailan Tirajoh
  • 481
  • 5
  • 17
0

thxs for all try helping . i solved it Path should be between " " ,

like this code and it worked good now ,

    public function deleteBrand($request){

    $deletelogo = $this->brandyModel::where('id',$request->id)->first();

    $path = public_path("upload/brands/$deletelogo->logo");
    if (is_file($path)){
    unlink($path);
    }

    $deletelogo->delete();
    $notificat = array(
        'message' => 'Brand Deleted Successfully',
        'alert-type' => 'success',
    );
    return redirect()->back()->with($notificat);
}
Я Диша
  • 1
  • 1
  • 5