Sorry the code was setup properly before I pasted here, I tried to strip it down just to see why it's throwing the "Trying to get property of non-object" error.
I used the Auth check in your second code block but it's still giving me the error. I don't have anything else, here's the entire routes.php file
<?php
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Welcome Controller
|--------------------------------------------------------------------------
|
*/
Route::get('/', function()
{
if (Auth::check())
{
$user = Auth::user(); // this is necessary as you don't use the variable $user
$userEmail = Auth::user()->email;
return 'you must log in';
}
});
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Here is my Model
User.php
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
// A user can have many answers
public function answers()
{
return $this->hasMany('App\Question');
}
}