Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to get property 'user_id' of non-object (new install with teams) #376

Closed
wouterdorgelo opened this issue Oct 22, 2020 · 13 comments
Closed

Comments

@wouterdorgelo
Copy link

  • Jetstream Version: ^1.5
  • Jetstream Stack: Inertia & Livewire (I tried both)
  • Laravel Version: ^8.0
  • PHP Version: ^7.3
  • Database Driver & Version: 10.4.14-MariaDB

Description: ErrorException on fresh Jetstream install: Trying to get property 'user_id' of non-object

Steps To Reproduce:

  1. laravel new playground --jet
  2. select stack
  3. php artisan migrate
  4. php artisan db:seed
  5. log in
  6. see error
@StanBarrows
Copy link

StanBarrows commented Oct 22, 2020

@wouterdorgelo

Where do you create your user? Only creating a User within your seeder is not enough. You also need a Team. Can you follow the registration process on the app and then log in?

Cheers
Sebastian

@driesvints
Copy link
Member

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

@ejntaylor
Copy link

Also need to seed a test team like so:

Team::create([ 'user_id' => 1, 'name' => 'admin-team', 'personal_team' => '1', ]);

@amrography
Copy link

amrography commented Nov 26, 2020

Check if user has team in the blade view.

resources/views/navigation-dropdown.blade.php

In line 61:

- @if (Laravel\Jetstream\Jetstream::hasTeamFeatures())
+ @if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->current_team_id)

In line 162:

- @if (Laravel\Jetstream\Jetstream::hasTeamFeatures())
+ @if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->current_team_id)

@QBLes
Copy link

QBLes commented Jan 25, 2021

i seem to be having this same problem.. i it was all working when i pushed to my github and later i downloaded a new copy it kept showing me these errors in my navigation-dropdown.blade.php i added in some checks for new menus and stuff like this

@if (Auth::user()->hasTeams())
@if($user->hasTeamPermission($team, 'admin'))

i mananged to get "hasTeams" to work again some how but hasTeamPermissions seems to always come back true when its not true.

its like the hole thing has bug somehwere from the pull form git..

@JonahKlimack
Copy link

Um.

Same problem. It's only on production for me.
Which, and other clues in here, leads me to believe it's a database issue.
production had 0 entries in Teams table.
local had one.
So I created the same entry on production
now it works.

Maybe I missed a php artisan migrate or something.

Hope this helps someone.

@QBLes
Copy link

QBLes commented Feb 25, 2021

@JonahKlimack for me it was because i have disabled having teams by default as i have extra code on signup to only make teams for one kind of signup... but my fix was the following..

In the AppServiceProvide.php (App/Providers) i added the following.. i could make it smaller but it works so i left it..

    view()->composer(['navigation-dropdown'], function ($view) {

        // Check if user owns a team...
        if (Auth::user()->hasTeams()) {

            $team = Team::where('user_id', auth()->user()->id)->first();
             
            // if they dont own a team are they apart of one..
            if ($team == NULL) {

                $team = Team::where('id', auth()->user()->currentTeam->id)->first();

            }

        } else {

            $team = '';

        }

        $view->with('team', $team);

    });

@DePalmo
Copy link

DePalmo commented Apr 6, 2021

Hello @driesvints,

I came here because I was following this tutorial: https://laracasts.com/series/laravel-authentication-options/episodes/2
And to make it clear, I was doing a clean installation of everything, Laravel, Breeze and then Jetstream. Did follow the installation instructions exactly, but it's not working.

So this is a bug with the library because it's not working out of the box, but needs to be fixed immediately, even before first run.

Please reconsider your position and push a fix for it.

@ArielMejiaDev
Copy link
Contributor

Hi @DePalmo you need to run migrations first, this is because the first page "/" and "dashboard" view are already using data related to user and teams, so that is why you are required to run migrations first, it is pretty much the same as trying to register a user with "Breeze" or "Laravel UI" without running the migrations, it would throw an exception, just in this case on the first page.

@DePalmo
Copy link

DePalmo commented Apr 6, 2021

For the sake of argument, I just tried again. Another clean installation but this time only Laravel and Jetstream. Steps:

  • laravel new test
  • updated .env with DB credentials and database name
  • composer require laravel/jetstream
  • php artisan jetstream:install livewire --teams
  • added .htaccess that redirects / to /public
  • npm install
  • npm run dev
  • php artisan migrate
  • register new user
  • error as mentioned in first post
    If I follow Trying to get property 'user_id' of non-object (new install with teams) #376 (comment), then there's no error but also no Teams in dropdown menu, I need to create first team manually directly in database for the menu items to show.

Please note that in first attempt, I was installing Laravel first, then Breeze and last Jetstream. I did run php artisan migrate:fresh afterwards, but same issue.

@QBLes
Copy link

QBLes commented Apr 6, 2021

@DePalmo for me i have it setup so i have 2 types of users, one being normal users with no teams and based on option clicked in signup i have "networks" this user type makes a team on the signup, so i guess as long as there is a team made then it should show in the dropdown.. here is an example of my App/Fortify/CreateNewUser.php

This also has a few edits for extra fields and such linking networks table to to teams and also to users ect..

class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;

public function create(array $input)
{
    //check if its a user account
    if($input['type'] == "user"){

        Validator::make($input, [
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(),
            'username' => ['required', 'unique:users', 'string', 'max:255'],
        ])->validate();

        $ip = $_SERVER['REMOTE_ADDR'];
        $user = User::create([
            'username' => $input['username'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
            'ip' =>  $ip,
        ]);
                
        return $user;
        
    }
    //check if its a Network account
    if($input['type'] == "networkname"){

        Validator::make($input, [
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(),
            'networkname' => ['required', 'string', 'max:255'],
            'username' => ['required', 'unique:users', 'string', 'max:255'],
        ])->validate();
        
        $ip = $_SERVER['REMOTE_ADDR'];
        $user = User::create([
            'username' => $input['username'],
            'email' => $input['email'],
            'password' => Hash::make($input['password']),
            'ip' =>  $ip,
        ]);

        if (isset($user->id)){

                //Add the Network
                $name = $input['networkname'];
                $userId = $user->id;
                $slug = Str::slug($input['networkname']);
                DB::table('networks')->insert(
                    ['name' => $name, 'slug' => $slug, 'user_id' => $userId, 'created_at' => now(), 'updated_at' => now()]
                );
                $network = DB::table('networks')->where('user_id', $userId)->first();
                //Make the Team for the Network
                DB::table('teams')->insert(
                     ['user_id' => $userId, 'name' => $name."'s Team", 'personal_team' => false, 'network_id' => $network->id, 'created_at' => now(), 'updated_at' => now()]
                );
             
        }
        return $user;

    }

}
}

@satoved
Copy link

satoved commented Apr 24, 2021

Read the docs carefully: https://jetstream.laravel.com/2.x/features/teams.html

Jetstream's team features allow each registered user to create and belong to multiple teams. By default, every registered user will belong to a "Personal" team. For example, if a user named "Sally Jones" creates a new account, they will be assigned to a team named "Sally's Team". After registration, the user may rename this team or create additional teams.

You cannot have a user without a team in the default --team scaffolding.

Just use withPersonalTeam() from UserFactory.

@PrabuKannan-DEV
Copy link

Faced the same issue, check the routes. I had the '/' route changed to point to a custom blade which requires authenticated user. Changed it back and it started to work.

I hope it helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests