Search code examples
laravelavatar

update user avatar with laravel5


From the look of it seems to me very logic but I am missing something, it doesn't work, nothing changes!

Here is my profile controller file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use Image;

class ProfileController extends Controller
{
    public function profile()
    {
        $user = Auth::user();

        return view('profile')->with('user', $user);
    }

    public function edit()
    {
        $user = Auth::user();

        return view('edit')->with('user', $user);
    }

    public function update(Request $request)
    {
        if($request->hasFile('avatar'))
        {
            $avatar = $request->file('avatar');

            $filename = time().'.'.$avatar->getClientOriginalExtension();

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

            $user->avatar = $filename;

            $user->save();
        }
        return redirect('profile')->with('user', Auth::user());
    }
}

and here is my edit.blade.php

@extends('layouts.app')
@section('content')
    <div class="col-md-6">
        {!! Form::model($user, ['method'=>'PATCH', 'action'=>'ProfileController@update', 'file'=>'true']) !!}

        <div class="form-group">
            {!! Form::label('name', 'Name') !!}
            {!! Form::text('name', null, ['class'=>'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::label('email', 'Email') !!}
            {!! Form::email('email', null, ['class'=>'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::label('number', 'Phone') !!}
            {!! Form::text('number', null, ['class'=>'form-control']) !!}
        </div>

        <div class="form-group col-md-5">
            {!! Form::label('avatar', 'Avatar') !!}
            {!! Form::file('avatar', ['class'=>'form-control']) !!}
        </div><br><br><br><br>

        <div class="form-group">
            {!! Form::submit('Update', null, ['class'=>'btn btn-primary']) !!}
        </div>

    {!! Form::close() !!}
    </div>
@stop

but when I edit the user it doesn't change..plz help


Solution

  • It seems like you have made a mistake while using Form from Laravel HTML Collective. You should use "files" => true instead of "file" => true

    {!! Form::model($user, ['method'=>'PATCH', 'action'=>'ProfileController@update', 'file'=>'true']) !!}