cugurel's avatar

Adding Profile Picture in Laravel

Hello i want to upload a profile picture and my database is this;

Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('surname'); $table->string('title'); $table->string('email')->unique(); $table->string('avatar')->default('default.jpg'); $table->string('password'); $table->rememberToken(); $table->timestamps(); });

my UserController is that;

class UserController extends Controller { public function profile() { return view('profile/{{surname}}',array('user'=>Auth::user()); }

public function update_avatar(Request $request)
{
    if($request->hasFile('avatar'))
    {
        $avatar=$request->file('avatar');
        $filename=time().'.'.$avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(300,300)->save(public_path('/uploads/avatars/'.$filename));

        $user=Auth::user();
        $user->avatar=$filename;
        $user->save();
    }
    return view('profile/{{surname}}',array('user'=>Auth::user());
}

}

But in conclusşon i can'get the default image.

Can u please help me?

0 likes
7 replies
Snapey's avatar

I would change this;

    Image::make($avatar)->resize(300,300)->save(public_path('/uploads/avatars/').$filename);

move filename out of the public path helper

also, this looks wrong, but I dont know what you are trying to do?

return view('profile/{{surname}}',

You need to mention how far you have got and what is not working

cugurel's avatar

I change it to profile. and i changed above .$filename but still it is not working...

@Snapey

Snapey's avatar

'not working' is not something I can help with

cugurel's avatar

i have many users then i have to make specific profile pages. profile/{{username}} is calling the user with surname. That's why i forward the controller to profile{{surname}}

Snapey's avatar

no its not, you are trying to load a view called literally 'profile/{{surname}}'

username or surname either way, its not going to work.

Dont confuse views with routes

cugurel's avatar

then how can i do that can u show a way to do this.

Snapey's avatar

Forget images. Learn some basics. Sorry for being blunt but if you don't know how to setup CRUD you are a long way from adding images.

cugurel's avatar

Okey sir. Thanks for suggestion. At the same time you don't have to be sorry because we have to be always blunt. :)

@Snapey

Please or to participate in this conversation.