Reputation: 65
I have this error when i trying to delete an image from db: Undefined variable: removeslider
public function delete($id){
$getslider = DB::table('slider')->where('id',$id)->get();
foreach($getslider as $getslider) {
$removeslider = $getslider->bgimage;
}
Storage::disk('uploadssliders')->delete($removeslider);
return redirect('admin/inicio');
}
Upvotes: 2
Views: 80
Reputation: 862
Try this:
public function delete($id)
{
$getslider = DB::table('slider')->where('id',$id)->first();
if($getslider){
Storage::disk('uploadssliders')->delete($getslider->bgimage);
return redirect('admin/inicio');
} else {
//id has no match in the database
echo "the id ". $id . " does not exist";
}
}
I changed your get()
to first()
to only get one result as this seems what you are trying to do then the foreach
is not needed.
Upvotes: 1