dimanche 29 avril 2018

Issue updating user account with avatar laravel

So I was able to submit the form to allow an avatar to be uploaded and changed which worked. Now I am trying to allow all the user details to be updated too.

If I just try to change the username or email and submit, the page is reloaded with the original content. When I upload a new avatar and then try to submit I get the error "Non-static method Illuminate\Http\Request::input() should not be called statically".

web.php

Route::get('profile','userController@profile');
Route::post('profile', 'userController@update_avatar');

userController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;

use Image;

class UserController extends Controller
{
    //
    public function profile()
    {
        return view('profile', 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->name = Request::input('username');
            $user->email = Request::input('email');
            $user->save();

        }
        return view('profile', array('user' => Auth::user()) );
    }
}

profile.blade.php

<img src="/uploads/avatars/" style="width:150px;height:150px;float:left;border-radius:50%;margin-right:25px">    
                <h2>'s Profile</h2>

                <form enctype="multipart/form-data" action="/profile" method="post">
                    <label>Update Profile Image</label>
                    <input type="file" name="avatar">
                    <input type="hidden" name="_token" value="">

                    <label>Username</label>
                    <input type="text" name="username" class="form-control" value="">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" value="">

                    <input type="submit" class=" btn btn-sm btn-light" style="color:#2b2b2b;">
                </form>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire