Search code examples
laravelimageregistrationprofile

Upload Profile Picture in Laravel Registration failed


I want to upload a profile picture at the registration

Before i had to register the user and after that i could change the default user.jpg into my own profile picture

but now i want to upload my profile picture at the registration

this is the part of my view where i want to upload the picture:

<div class="form-group col-md-12">
                <label>Firmenlogo</label>
                <div class="custom-file">
                    <input type="file" name="avatar" class="custom-file-input" id="validatedCustomFile" required>
                    <input type="hidden" name="_token" value="{{csrf_token()}}">
                    <label class="custom-file-label" for="validatedCustomFile">Datei auswählen...</label>
                    <div class="invalid-feedback">Bitte laden Sie hier Ihr Firmenlogo hoch.</div>
                </div>
            </div>

and this is my controller:

<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{

use RegistersUsers;

protected $redirectTo = '/admin';

public function __construct()
{
    $this->middleware('guest');
}

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'avatar' => 'required|string',
    ]);
}

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'avatar' => $avatar,
    ]);
}

}

this is my migration file:

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('avatar')->default('default.jpg');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
    return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

and this is my user.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    protected $fillable = [
        'name', 'email', 'password', 'avatar',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

so everyythign works it saves the pciture as a string in my database like picture.jpg but it dont save it in my uploads folder so i try this:

$avatar = $data['avatar']->file('avatar');
    $filename = time() . '.' . $avatar->getClientOriginalExtension();
    Image::make($avatar)->resize(300, 300)->save(public_path('/uploads/avatars/'.$filename));

but nothings happened it doesnt save anything in my database if i click on register the page reload and nothing happens i dont know why and i dont know what to do i tried so many things and read so many blogs but nothing works

it only works after the registration if i go to my profile and upload a new picture

but why?? so i know the code works but why not in the register controller?


Solution

  • AH I GET IT! such a dumb thing!!!!!!

    protected function validator(array $data)
        {
            return Validator::make($data, [
                'name' => 'required|string|max:255',
                'email' => 'required|string|email|max:255|unique:users',
                'password' => 'required|string|min:6|confirmed',
                'avatar' => 'required',
         ]);
        }
    

    in this function i had avatar required and STRING and after i delete that everything works!!!! OMG so happy now

    this is my create function now:

     protected function create(array $data)
    {
        $request = app('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) );
        }
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'avatar' => $filename,
        ]);
    }