can you dd(Auth::user());
yeah it's still returning "null"
Can you post your full routes file again, your UsersController or whatever you use (if anything), your User model, and your Users table migration along with a screen dump of data in your users table using Sequel Pro or something similar.
Currently I'm more sure what is going on, but it could be that you're being logged out at some point.
Ok I will try, to get that all together
Hmm, if I'm being logged out though, why would
$id = Auth::id();
return $id;
return the user's id, i've logged in as different users and it has returned the correct user IDs.
And I assume that you are running the following right underneath that code?
return Auth::user()->email; //
Does the following produce null?
$user = Auth::user();
return $user->email;
Ya, it gives me the same error "Trying to get property of non-object"
Any time I use Auth::user it throws that error.
Extract your Route closures to a controller, let's say UsersController for now. The code below works for me, so if it doesn't work for you then there is something else going on. So, that will look like:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class UsersController extends Controller {
public function index() {
return "show all users";
}
public function show() {
return Auth::user()->email;
}
}
Then in your routes.php
Route::get('user', ['uses' => 'UsersController@show']);
Ok I tried that as well, still "Trying to get property of non-object"
Then there is something else wrong in your code.
Have you tried a clean install of Laravel. We should check your environment.
And can you post all your code as I asked before, it could be something simple.
I created a fresh install of Laravel, I created a new database and ran the default migration. Everything is default except for the controller and route that you suggested & I created one user. I attempted to log in (auth/login) and get this error.
PDOException in Connector.php line 47:
SQLSTATE[28000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)
Why is it trying to connect to homestead database when my .env file looks like this:
DB_HOST=localhost
DB_DATABASE=dblaratest
DB_USERNAME=root
DB_PASSWORD=root
Are you using Homestead? If so, the username and password is homestead and secret respectively.
Create a new database, and use that, so it is completely clean and then try again.
No I'm not using Homestead, I'm using MAMP Pro. And yes I did create a clean database.
It should use your .env variables. In your routes file can you dd(Config::get('DB_DATABASE'));
Change the database credentials in the .env file to match your MAMP credentials.
@Ruffles that is what he has done.
Oh yeah, I skipped that.
@thinkjay any progress?
@mstnorris i updated my MAMP PRO, and recreated the project, one file at a time, and it seems to be working properly now thanks again for all your help and following up!
@mstnorris I just ran into this as well. The simple fix that I found was it matters what order the middleware is loaded. For instance I have a SimpleAdmin middleware that checks a flag in DB (I'll dev that more later) for a simple way to check if a user has super_admin access. That middleware was running before 'web' and 'auth'. Swapped the order and all is good.
here is problem that if u logged in as user so that page should under auth middleware so in this case in controller it should like
Route::get('/', function()
{
if (Auth::check())
{
$user = Auth::user();
$userEmail = Auth::user()->email;
return $userEmail;
}
else {
$user = Auth::user();
$userEmail = Auth::user()->email;
return $userEmail;
}
})->middleware('auth');
Please or to participate in this conversation.