Reputation: 994
Hello guys I need some help from you guys. I am creating a laravel project where I have modified the default users table to store more informations of users like name and user image file name. So that I had to modify 'RegisterController' class where 'create' function has been modified and I have also added an 'update' method to store the uploaded image file. Below is my code of 'RegisterController' class:
protected function update(array $data)
{
$path = $data['img']->storeAs('/public','ohgod ');
return $path;
}
protected function create(array $data)
{
$filename;
$filename = $data['img']->getClientOriginalName();
$filesize = $data['img']->getClientSize();
$this->update($data);
return User::create([
'name' => $data['name'],
'img_name' => $filename,
'img_size' => $filesize,
'username' => $data['username'],
'dob' => $data['dob'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
With this I also had to modify the form to get add informations of the user. And now when I register as a new user.I get every thing right in users table but image file is not stored in the server.
I am new in laravel so I don't know whats wrong with my code. I also haven't got any error messages. So help me guys.
I got the file storing code from the official site of laravel https://laravel.com/docs/5.5/filesystem
Upvotes: 1
Views: 634
Reputation: 8078
You have move image to folder before inserting into db
$destinationPath='images';
$filename = $data['img']->getClientOriginalName();
//move iamge to folder
$fileName = str_random(30).'.'.$data['img']->clientExtension();
$data['img']->move($destinationPath, $fileName);
moved file will be available in public/images folder
Upvotes: 2
Reputation: 154
you can go to Illuminate\Foundation\Auth\RegistersUsers trait and put your image code into register method
public function register(Request $request)
{
$this->validator($request->all())->validate();
$fields = $request->all();
if ($request->hasFile('avatar'))
$fields['avatar'] = $request->avatar->store('users', ['disk' => 'public']);
$user = $this->create($fields);
event(new Registered($user));
$this->guard()->login($user);
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
then go to App\Http\Controllers\Auth\RegisterController and append image into data array in the create method
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'avatar' => $data['avatar'],
'headline' => $data['headline'],
'summary' => $data['summary'],
]);
}
Upvotes: 1
Reputation: 470
$image = time().'.'.$data['img']->getClientOriginalExtension();
$data['img']->move(public_path('images'), $image );
best way to store image in public images folder before sending to db.
Upvotes: 0
Reputation: 87
If you are using laravel
form then use
{!! Form::open(['url' => url('background'), `'files'=>true,` 'method' => 'post', 'class' => 'form-horizontal']) !!}
and if you are using simple html form then use below code
<form enctype="multipart/form-data"><form>
i hope it works for you.
Upvotes: 0