What is going on here?
Feb 8, 2018
7
Level 2
Unable to upload image in RegisterController.php
public function uploadfile(){
$rules = array(
'file' => 'required | mimes:jpeg,jpg,png | max:1000',
);
$request=new Request();
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
}
$file = $this->$request->file('file')->store('public');
return $file;
}
protected function create(array $data)
{
$file=self::uploadfile();
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'city'=> $data['city'],
'country'=>$data['country'],
'type'=>$data['type'],
'file'=>$data['file']
]);
}
Uploading a file like php7AEB.tmp
Level 53
In that case, just make sure you save the correct path to the DB:
$file=$this->uploadfile(); // Use $this-> instead of self::
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'city'=> $data['city'],
'country'=>$data['country'],
'type'=>$data['type'],
'file'=>$file // This is the actual path after store() has done it's work
]);
Just remember, with your code as it is now, $file could also be a redirectResponse instead of a filepath, which would break the User::create() command.
You might want to check into the other changes I proposed because right now you're not checking anything (I'm even amazed that it somehow passes the validation in uploadFile() when you're validating a newly created empty Request object)
Please or to participate in this conversation.