I am trying to delete a file after I delete the product from the database, but it shows me an error, and I cannot find a solution.
Call to a member function delete() on null
public function supprod($id,Request $request)
{
$produits=Produit::find($id);
$produits->delete(); // <-- error on this line
$filename=(string)$document->nom;
Storage::disk('fichier')->delete($filename);
return redirect()->back();
}
this is check the record this function $produits=Produit::find($id);
This use $produits=Produit::where('id',$id)->first();
You should really let Laravel do the work for you.
Change your action to:
public function supprod(Produit $id, Request $request) {
$filename=(string)$id->nom;
Storage::disk('fichier')->delete($filename);
$id->delete();
return redirect()->back();
}
This will make the framework find the appropriate model or throw a 404 error if it does not exist.
More information in the implicit model binding section of the documentation