Auth::user returns random user object - laravel

I'm having a very strange problem that I don't know what the cause could be.
I have a Laravel 7 project configured with the standard Auth Scaffolding. This was working without issue until just recently. I noticed I was unable to login to my project (redirected back to home).
During my investigation of what the cause could be I created a test route to return
Auth::check();
Auth::check would return true even though no one was logged in.
I did another check with
Auth::user();
This is where things get weird. Here is a sample output of Auth::user()
{
"first_name":"Titus",
"last_name":"Senger",
"email":"casper.kuhic#example.org",
"username":"vwolff",
"country":"pa",
"biography":"Sed quod ad corporis quasi. Repellat tempora quo ab ut. Molestiae iure qui illo recusandae rerum iure vitae quasi. Odit ipsam ut laudantium et sed consequuntur ab non.",
"category_id":3,
"social":{
"facebook":"",
"instagram":"",
"twitter":"",
"linkedin":"",
"youtube":""
},
"avatar":[
],
"banner":[
]
}
It appears to be faker data of a user object but with no user id. If I refresh the route, I get another user object returned with completely different user data.
I thought maybe this could be returning random user data in my database as I did use a seeder/factory to create a bunch of random user accounts for testing my app. I ran
php artisan migrate:refresh
Now I confirmed that all my tables were empty, including the users table. Navigated to my test route and sure enough, still generating random user object on each refresh of Auth::user().
What is happening!? I have no idea what would have caused this. This is not a production build yet. Everything is still being worked on locally.
I have also tried rolling back several commits from github to where I know Auth was working properly and the issue seems to persist even with older versions of my code.
composer.json
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.2.5",
"doctrine/dbal": "^2.10",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^1.0",
"guzzlehttp/guzzle": "^6.5",
"laravel/cashier": "^12.1",
"laravel/framework": "^7.0",
"laravel/tinker": "^2.0",
"laravel/ui": "^2.0",
"nassajis/laravel-resumable-upload": "^1.2",
"php-ffmpeg/php-ffmpeg": "^0.16.0",
"predis/predis": "^1.1",
"pusher/pusher-php-server": "^4.1",
"spatie/laravel-medialibrary": "^8.0.0",
"spatie/laravel-permission": "^3.13",
"stripe/stripe-php": "^7.45",
"ticketpark/wsc-rest": "^1.2"
},
"require-dev": {
"facade/ignition": "^2.0",
"fzaninotto/faker": "^1.9.1",
"laracasts/generators": "^1.1",
"mockery/mockery": "^1.3.1",
"nunomaduro/collision": "^4.1",
"phpunit/phpunit": "^8.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
}
}
EDIT
The only thing that I recently did that may or not somehow influence this issue is changing session_driver to use database and run a migration for sessions table. I have rolled back the change and updated my session_driver to use file again, but I am still having problems.
Additional Things I've tried doing:
php artisan cache:clear
php artisan config:clear
Returning User::all() retrieves an empty array []
EDIT 2
It appears Auth::user() is some how looking at my factory as I have now commented out the user factory and receive this error when trying to view my test route:
Unable to locate factory for [App\User].
I also cannot restart my server or clear cache anymore because I receive the same error:
Unable to locate factory for [App\User].
at C:\xampp\htdocs\html\sodisting\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:273
269| */
270| protected function getRawAttributes(array $attributes = [])
271| {
272| if (! isset($this->definitions[$this->class])) {
> 273| throw new InvalidArgumentException("Unable to locate factory for [{$this->class}].");
274| }
275|
276| $definition = call_user_func(
277| $this->definitions[$this->class],
1 C:\xampp\htdocs\html\sodisting\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:296
Illuminate\Database\Eloquent\FactoryBuilder::getRawAttributes([])
2 C:\xampp\htdocs\html\sodisting\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:155
Illuminate\Database\Eloquent\FactoryBuilder::Illuminate\Database\Eloquent\{closure}()

I found the issue. Turns out there was a reference in my AppServiceProvider to the factory. Some leftover test code that I completely forgot about. I feel stupid.
Thanks everyone for their help in trying to solve this with me.

Related

Laravel 9 is getting "PHP Fatal error: Uncaught ErrorException: Method ReflectionParameter::getClass() is deprecated in" after deployment

I build an aplication in Laravel 9 using Laravel Breeze and also vite. There is no problem with my application on my localhost (PHP 8.2). Problem stars when I try to deploy the application on server, I did everything normally according to Laravel Deployment documentation, but I am still getting this error from log on server: PHP Fatal error: Uncaught ErrorException: Method ReflectionParameter::getClass() is deprecated in and after this there is an list of files where this method is used I assume.
I googled this error, but every answer about this problem is only when someone was upgrading older project to PHP 8.0 or higher.
Here is also my composer file and I think I got everything in order:
`{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"php": "^8.0.2",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"livewire/livewire": "^2.10",
"nesbot/carbon": "^2.64"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
"laravel/breeze": "^1.15",
"laravel/pint": "^1.0",
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^6.1",
"phpunit/phpunit": "^9.5.10",
"spatie/laravel-ignition": "^1.0"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
],
"post-update-cmd": [
"#php artisan vendor:publish --tag=laravel-assets --ansi --force"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}`
Thanks.
I tried everything, including composer update and I double-checked my deployment method, which I am using for every project.
It is usually caused by a compatibility issue between the version of PHP on your server and the version of Laravel and its dependencies that you are using.
The ReflectionParameter::getClass() method has been deprecated in PHP 8.0 and removed in PHP 8.2.
To resolve this issue, you may need to update your application to a version of Laravel that is compatible with PHP 8.2 and its dependencies.
Additionally, you can try downgrading the PHP version on your server to a version that is supported by your application, or upgrading your application to a version that is compatible with the PHP version on your server.
It seems like in your local you are having PHP version less than 8.2 and in your server it is 8.2. You can check it by adding the following code to a route or controller in your Laravel application:
<?php
phpinfo();
Sorry guys, mistake was on my end :). I have two project (old one from 2017 and this new one) on one server. I just set root directory on this new domain to old version of project. :)

What would be the proper steps to upgrade laravel version 5 to latest version 9

I have an old project which I developed in Laravel 5.3 and I want to upgrade the project to Laravel v9. What are the steps to upgrade?
What I have seen so far on the internet and from my knowledge:
Upgrade to each higher version step by step e.g 5.3 to 5.4 ... 6 to 7 to 8 to 9
I am running php 8 on my macos. And currently, my code is not running because of deprecated methods in php 5.6 that my Laravel 5.3 is using.
How to upgrade properly?
My composer.json file is
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*",
"appzcoder/crud-generator": "^2.0",
"laravelcollective/html": "5.3.*",
"doctrine/dbal": "^2.5",
"maatwebsite/excel": "~2.1.0",
"predis/predis": "~1.0",
"guzzlehttp/guzzle": "~4.0",
"laravel/scout" : "^2.0",
"algolia/algoliasearch-client-php": "^1.17",
"fx3costa/laravelchartjs": "^2.2",
"gloudemans/notify": "^1.0",
"pda/pheanstalk": "~3.1"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.0",
"symfony/css-selector": "3.1.*",
"symfony/dom-crawler": "3.1.*"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist",
"allow-plugins": {
"kylekatarnls/update-helper": true
}
}
}
The latest dependencies can be found in composer.json from https://github.com/laravel/laravel.
There are many more things to do:
Traverse the repository above and check all files for changes with your own project. Often times they contain backwards compatible changes (like new keys in config/xxx.php files) but sometimes you need to change certain function definitions because names, parameters or interfaces have changed.
(optional) update the css/js build process since it has been revised many times (I believe the default build tool now is "Vite"; older implementations still use "Laravel Mix").
Traverse the upgrade guides https://laravel.com/docs/9.x/upgrade. You could start with all the high impact changes to fix the bulk of the issues. If you have enough knowledge of the framework you can probably just update the dependencies (skipping all upgrade guides), analyze the exception traces and work from there.
Test every functionality of your website (Laravel 5.3 is from 2016 so there could be quite some technical debt in fixing all errors).
Hopefully you have some tests written to automate some if this testing process :)
Note that this repository is the blueprint of a Laravel project. The actual framework resides in https://github.com/laravel/framework (it is the dependency that can be found in the composer.json above).
There is sadly no clearcut answer to do all this. If you want to do it slowly but surely you could traverse each and every upgrade-guide (and test in between) but it could take quite some time.

Class "Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory" not found on Laravel 9

I just wanted to use Mailgun to send E-mail from my Laravel project and followed this steps from official document: https://laravel.com/docs/9.x/mail#mailgun-driver
composer require symfony/mailgun-mailer symfony/http-client
When I try to send password reset e-mail to test it, it throws an excepiton:
Class "Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory" not found
Here is the full stack trace: https://flareapp.io/share/oPRKqyZ7#share
I don't know but maybe it's because this project started as a Laravel 8 project and I updated it to Laravel 9 one week ago. Is it trying to find something comes with Laravel 9 into app directory or something but my project doesn't have that? I didn't understand.
By the way if it helps; this project uses Jetstream with Inertia.js and Vue.js. So the composer.json looks like this now:
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"php": "^8.0.2",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.2",
"inertiajs/inertia-laravel": "^0.5.4",
"laravel/framework": "^9.2",
"laravel/jetstream": "^2.4",
"laravel/octane": "^1.0",
"laravel/sanctum": "^2.14.1",
"laravel/tinker": "^2.7",
"sentry/sentry-laravel": "^2.11",
"symfony/http-client": "^6.0",
"symfony/mailgun-mailer": "^6.0",
"tightenco/ziggy": "^1.0"
},
"require-dev": {
"spatie/laravel-ignition": "^1.0",
"fakerphp/faker": "^1.9.1",
"laravel/sail": "^1.12",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^6.1",
"phpunit/phpunit": "^9.5.10"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
],
"post-update-cmd": [
"#php artisan vendor:publish --tag=laravel-assets --ansi"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
From the upgrade guide:
To continue using the Mailgun transport, your application should require the symfony/mailgun-mailer and symfony/http-client Composer packages:
so use:
composer require symfony/mailgun-mailer symfony/http-client
More information here Upgrade to laravel 9
Apparently it was something with Laravel Octane which I use on my project. I already ran composer require symfony/mailgun-mailer symfony/http-client and composer auto-dump etc and restart my processes on Supervisor but nothing changed. In the and I killed all processes (including supervisor, which keeps Laravel Octane alive) and then restarted them.
I think Laravel Octane wasn't able to load renewed composer autoload files. When everything stopped and restarted, they just worked well.

ErrorException When Registering with Laravel 8 Jetstream

I started a project on Laravel 7 just before the launch of Laravel 8, I am still learning this framework, so I updated to that version using this steps, and I am aware of this new scaffolding library call Jetstream that I installed with these steps:
composer require laravel/jetstream
php artisan jetstream:install livewire --teams
npm install && npm run dev
php artisan migrate
After working on my views and models I am proceeding to implement sessions functionalities, but when I tried to register with the project at localhost:8000/register, and having my data validated I get this error
Trying to get property 'id' of non-object (View: \path-to\my-project\resources\views\navigation-dropdown.blade.php) (View: \path-to\my-project\resources\views\navigation-dropdown.blade.php) (View: \path-to\my-project\resources\views\navigation-dropdown.blade.php)
http://localhost:8000/dashboard
This is the section of view that is throwing the exception
navigation-dropdown.blade.php line 169 this is the vanilla file
<!-- Team Settings -->
<x-jet-responsive-nav-link href="{{ route('teams.show', Auth::user()->currentTeam->id) }}" :active="request()->routeIs('teams.show')">
{{ __('Team Settings') }}
</x-jet-responsive-nav-link>
The data introduced in the register form gets stored in the database, but the error happens
I checked this other question Laravel Jetstream + Livewire + Teams FeatureTest problems when authenticate but it seems that answer apply to a different context since it is dealing with tests, no the vanilla register functions.
This is my current composer.json
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.3.0",
"fideloper/proxy": "^4.2",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.0",
"laravel/jetstream": "^1.0",
"laravel/sanctum": "^2.6",
"laravel/tinker": "^2.0",
"livewire/livewire": "^2.0"
},
"require-dev": {
"facade/ignition": "^2.3.6",
"fzaninotto/faker": "^1.9.1",
"mockery/mockery": "^1.3.1",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^8.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
],
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
]
}
}
I tried installing to test a fresh Laravel project with Jetstream, and this worked as it should, that let me confused as to what happened with my current project.
In the end, what worked for me was create a new fresh project with jetstream, then in my original project deleted the vendor and node_modules folder then copying the files of the new fresh project unto my original project and now it works as expected
Answer added on behalf of OP.
The user was not created with a team assigned, see users table and column current_team_id is null.
The navigation-menu.blade.php checks only for TeamFeatures but not if a team is really assigned.
#if (Laravel\Jetstream\Jetstream::hasTeamFeatures())
#if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->current_team_id)
For details here is the issue trying to get property of non-object

Class 'Laravel\Passport\Passport' not found on Laravel 5.7

I just cloned a repo with a Laravel 5.7 project, and I am having some troubles to run it on my machine.
When I make a composer install, I get this error:
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
Nothing to install or update
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover
In AuthServiceProvider.php line 35:
Class 'Laravel\Passport\Passport' not found
Script #php artisan package:discover handling the post-autoload-dump event returned with error code 1
What can I do to solve this error? I dont know where to start.
This is the project's composer.json file:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": "^7.1.3",
"designmynight/laravel-mongodb-passport": "^1.1",
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "^6.3",
"jenssegers/mongodb": "^3.4",
"laravel/framework": "5.7.*",
"laravel/passport": "^7.0",
"laravel/tinker": "^1.0"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"dont-discover": [
]
}
},
"scripts": {
"post-root-package-install": [
"#php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"#php artisan key:generate"
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
Although composer update did the trick depending on situation it might not be the best solution.
First of all - when running composer install packages are installed based on versions that are in composer.lock if it's included in repository (it should be).
And in your case when you look carefully at console output you had:
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.
So it's quite possible that someone added Laravel Passport package to composer.json but haven't run composer install or forgot to commit updated composer.lock file.
Now remember when running composer update you update all the packages. Sometimes it's fine (only on localhost) but sometimes it's not because not always you want to update all packages. So when you need to update single package you could try in this case also composer update laravel/passport just to force updating/installing just Laravel Passport without touching other packages.
It looks like you need to do
composer update
instead of install?

Resources