How to get the name of the file in the database 'img' as $ fileName? in place of this in my database, the file name is indicated as pathname, like this "C: \ wamp64 \ tmp \ php709A.tmp"

function store(Request $request){ $this->validate($request, ['title'=>'required|max:255', 'desc'=>'required', 'price'=>'required', 'img'=>'required']); $fileName = null; if (request()->hasFile('img')) { $file = request()->file('img'); $fileName = md5($file->getClientOriginalName() . time()) . "." . $file->getClientOriginalExtension(); $file->move('img', $fileName); } $data = $request->all(); $product = new Product; $product->fill($data); $product->save(); dump($data); } 

2 answers 2

Replace the data that comes in the request with what you need:

 ... $data = $request->all(); $data['img'] = $fileName; $product->fill($data); .... 
  • excellent, thanks - Miqayel Petrosyan

There is a useful function: basename ()

You can set it on the result of the $ file-> getClientOriginalName () method