2
votes

If I use Auth::user()->username in a Blade file laravel returns me an empty String but Auth::user()->email is filled. I use my own AuthController and my Login,Register and Logout work perfectly but I can't get the username.

Routes.php

<?php

Route::group(['middleware' => ['web']] , function () {
    Route::get('/', function () {
        return view('welcome');
    })->name('home');
});

Route::group(['middleware' => ['web','guest']], function () {
    Route::auth();

     #Sign up Routes
    Route::get('/signup', function () {
        return view('auth.signup');
    })->name('auth.signup');
    Route::post('/signup', 'AuthController@signup');

    #Sign in Routes
    Route::get('/signin', function () {
        return view('auth.signin');
    })->name('auth.signin');
    Route::post('/signin', 'AuthController@signin');
});

Route::group(['middleware' => ['web','auth']], function () {
    Route::auth();

    #Sign out Routes
    Route::get('/signout', 'AuthController@signout')->name('auth.signout');
});

And my custom Auth Controller is:

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;

class AuthController extends Controller
{

  public function signup(Request $request) {
    $this->validate($request, [
        'email' => 'required|unique:users|email|max:255',
        'username' => 'required|unique:users|alpha_dash|min:2|max:20',
        'password' => 'required|min:6'
    ]);

    User::create([
        'email' => $request->input('email'),
        'username' => $request->input('username'),
        'password' => bcrypt($request->input('password')),
    ]);

    return redirect()->route('home');
  }

  public function signin(Request $request) {
    $this->validate($request, [
        'email' => 'required',
        'password' => 'required'
    ]);

    if(!Auth::attempt($request->only(['email', 'password']), $request->has('remember'))) {
        return redirect()->back()->with('flash-message','We can not sign you in with this data!'); 
    }

    return redirect()->route('home');
  }

  public function signout() {
    Auth::logout();

    return redirect()->route('home');
  }
}

Maybe someone can help me .

Note: I added the username into User.php under the filled array.

2
0. You don't need to add 2 'web' middleware route groups, you can put both 'guest' and 'auth' routes under one 'web' middleware. Where do you call Auth::user()->username?Davor Minchorov
I call it in my home.blade.php if the use is authenticated (Auth::check ())Daniel O.
so you have something like @if(Auth::check()) Auth::user()->username @endif?Davor Minchorov
Try grouping the routes under the web middleware instead of having 3 separate route groups using the same middleware.Davor Minchorov
I think that don not remive the problem that Auth::user ()->username is empty and email is fillesDaniel O.

2 Answers

2
votes

Most likely you are missing username in $fillable of your User model.

The create method only accept fields coming from $fillable.

0
votes

Please edit your User model like this:

protected $fillable = [
       'email', 'username', 'password',
    ];

Only $fillable fields insert by Create method.