Why uploadFile function is not working ? function uploadFile($file,$dir){
$dir = strtolower($dir);
$path = public_path().'/uploads/'.$dir;
if(!File::exists($path)){
File::makeDirectory($path, 0777, true);
}
$file_name = ucfirst($dir)."-".date('YmdHis').rand(0,9999).".".$file->getClientOriginalExtension();
$status = $file->move($path, $file_name);
if($status){
$file->save($path."/".$file_name);
}
}
My Problem is : File is uploaded on the directory but it is now showing on database 'file' column. Is there any mistake on above code ? Please help.
Hi @santoshdc your problem is here
$file->save($path."/".$file_name);
$file is uploaded file object, not the model. To store in database you should set value for related model
Thank you for replying. I have written my code on controller like this.
public function store(Request $request){
$rules = $this->notify->validateRules();
$request->validate($rules);
$data = $request->except('file');
if($request->file){
$file_name = uploadFile($request->file, 'notify');
if($file_name){
$data['file'] = $file_name;
}
}
$data['created_by'] = $request->user()->id;
$this->notify->fill($data);
$status = $this->notify->save();
if($status){
$request->session()->flash('success', 'Notification created successfully.');
}else{
$request->session()->flash('error', 'Sorry ! Notification could not be created at this time.');
}
return redirect()->route('notify.index');
}
But still getting same problem 'null' value on database. Can you please explain little bit more so that I can understand properly.
@santoshdc as I see, your function uploadFIle doesn't have return statement, but you expect file_name as the result of the function in controller to set file name
Thank you so much. Literally It took me 3 hours to figure out that problem. Now problem is solved.
You shouldn't have to move and Save use one or the other.
Please sign in or create an account to participate in this conversation.