dib258's avatar
Level 11

Adding picture when register

Hi,

So, I'd like that, when a user register, he can add a profile picture. I made the new input in the template and I filled the create method in AuthController.php :

protected function create(array $data) {
    $fileName = 'null';
    if (Input::file('image')->isValid()) {
        $destinationPath = public_path('uploads/files');
        $extension = Input::file('image')->getClientOriginalExtension();
        $fileName = uniqid().'.'.$extension;

        Input::file('image')->move($destinationPath, $fileName);
    }

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'image' =>  $fileName,
        'password' => bcrypt($data['password']),
    ]);
}

When I try it the image upload correctly to the right folder but when I use thinker to see if the picture name is in the DB. There's nothing :

$user = App\User::OrderBy('id', 'desc')->first(); => App\User {#689 id: 5, name: "John Doe", email: "john.doe@gmail.com", image: "", created_at: "2015-12-03 11:48:12", updated_at: "2015-12-03 11:48:12", }

I tried many times and I can't understand why the picture name wont insert in the DB. I tried to replace the $fileName in the return user creation by "test" but nothing also. Is it the right method called when the registration is called ?

I would think so since the new update in laravel RegistersUsers.php page's postRegister method call the create method I used :

public function postRegister(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    Auth::login($this->create($request->all()));

    return redirect($this->redirectPath());
}

Thanks in advance for your help !

0 likes
8 replies
bestmomo's avatar
Level 52

Did you add it in $fillablearray in User model ?

1 like
dib258's avatar
Level 11

Damn you're right ! I forgot to add the image in this array...

Thanks!!! :)

1 like
amosmos's avatar

Hey quick question -

On the postRegister method you call the validator - what happens if the request doesn't validate? How do you show the user his "old", chosen profile image?

herrygallery's avatar

I wanna do the same. At first, I try with the usual $request->file('image') etc etc.. but didnt work.

hridoy100's avatar

You can store images this way:

$user =  User::create([
        'first_name' => $data['first_name'],
        'last_name' => $data['last_name'],
        'phone' => $data['phone'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'gender' => $data['gender'],
        'job_title' => $data['job_title'],
        'department_id' => $data['department_id'],
        'branch_id' => $data['branch_id'],
        'section_id' => $data['section_id'],
    ]);

    if (request()->hasFile('profile_picture')) {
        $file = request()->file('profile_picture');
//            dd($file);
        $extension = $file->getClientOriginalExtension();
        $filename = $user->first_name . $user->last_name . '_' . $user->id . '.' . $extension;
        $file->move('img/profile/', $filename);
        $user->profile_picture = 'http://127.0.0.1/img/profile/'.$filename;
        $user->save();
    }

return $user;
jasminchopra125's avatar

sometimes browsing through the forum also helps, I was having a similar problem and find out the solution

Please or to participate in this conversation.