laravel / jetstream

Tailwind scaffolding for the Laravel framework.
https://jetstream.laravel.com
MIT License
3.95k stars 809 forks source link

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

Closed wouterdorgelo closed 3 years ago

wouterdorgelo commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

Also need to seed a test team like so:

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

amrography commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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.