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

Error when user dont have any teams #188

Closed
youyi1314 opened this issue Sep 14, 2020 · 12 comments
Closed

Error when user dont have any teams #188

youyi1314 opened this issue Sep 14, 2020 · 12 comments

Comments

@youyi1314
Copy link

  • Laravel Version: ^8.0
  • PHP Version: 7.3.20
  • Database Driver & Version: Mysql 5.7.31
  • Install methods: Laravel Installer
  • Installer Version v4.0.3

Description:

Install Command : laravel new --jet --teams
composer require laravel/jetstream
php artisan jetstream:install livewire
npm install && npm run dev
php artisan vendor:publish --tag=jetstream-views
php artisan migrate

Added use HasTeams; to user models

When login with a non-team user, it will show an error.

Steps To Reproduce:

I just start up and create 1 user using factory with data below:

return [
            'name' => $this->faker->name,
            'username' => $this->faker->unique()->userName,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];

and tried also register at register page.
after login to dashboard page, it will show Trying to get property 'id' of non-object
that error at: vendor/laravel/jetstream/src/HasTeams.php:28

@driesvints driesvints transferred this issue from laravel/framework Sep 14, 2020
@driesvints
Copy link
Member

Transferred to the jetstream repo.

@driesvints driesvints changed the title [Laravel 8] Error when user dont have any teams Error when user dont have any teams Sep 14, 2020
@taylorotwell
Copy link
Member

How do you login with a "non-team" user? All registered users receive teams on sign up.

@youyi1314
Copy link
Author

youyi1314 commented Sep 14, 2020

How do you login with a "non-team" user? All registered users receive teams on sign up.

@taylorotwell Thanks for your reply,
i think i got bug, cos i register but no team is added, or may i know how it create team? cos i tried both add user manually with terminal and from register form. no team is added.

@sureyeahdie
Copy link

How do you login with a "non-team" user? All registered users receive teams on sign up.

@taylorotwell Thanks for your reply,
i think i got bug, cos i register but no team is added, or may i know how it create team? cos i tried both add user manually with terminal and from register form. no team is added.

Encountered the same problem when I manually create user using factory (UserFactory.php).
You can bypass this by inserting a new row to teams table on your db. Then refresh the page.

As for register, It's working fine for me.

@iRajul
Copy link

iRajul commented Sep 26, 2020

@sureyeahdie How can we create a user with team using factory ??
Currently I am using User::factory()->create() , but it seems it is not creating teams

@sureyeahdie
Copy link

@sureyeahdie How can we create a user with team using factory ??
Currently I am using User::factory()->create() , but it seems it is not creating teams

@iRajul the default factory doesn't handle other data creation, you need to create another factory that handles teams creation. Make sure you provide the factories with some static values, as the default UserFactory uses the Faker Library to populate data.

More about factories and db seeding:
https://laravel.com/docs/8.x/database-testing#creating-factories

@EPGDigital
Copy link

I can see 2 issues here:

  1. If jetstream provides teams, should there be a TeamFactory and UserFactory ready to use with a team, especially if behaviour is must have team
  2. I think there would be use cases for a user not existing in a team (e.g. User registers on a site, then picks a team to join), so jetstream should probably deal with the error nicely, rather than assuming always a team assigned.

Both of these can be handled in code by the developer, but should they happen/be available by default?

@VKambulov
Copy link
Contributor

How do you login with a "non-team" user? All registered users receive teams on sign up.

@taylorotwell, I want to give an example when creating a user without a team can be useful.
At the moment, when developing an application, I want to use teams as organizations, which will include users. Users, in turn, after filling out the registration form, must authorize on the site, but see only the most basic set of information, without having any more rights, until the super-administrator or special manager checks his registration application and assigns him a team and a role in this team.
I would suggest taking into account similar situations in the package and allowing the user to register without creating a team for him automatically. And give the ability to authorize a user without a team assigned to him.
It is also possible that commands are used as teams, for example, sports, and users without assigned teams will be observers.
Maybe what I said above contradicts the principles for which you created teams, but I believe that the approach I described can be very useful for many developers.
Thank you for your time.

@VKambulov
Copy link
Contributor

Maybe someone will need a solution in the future:

  1. You need to overwrite method switchTeam in User model:
    /**
     * Switch the user's context to the given team.
     *
     * @param $team
     * @return bool
     */
    public function switchTeam($team): bool
    {
        if (!$team || !$this->belongsToTeam($team)) {
            return false;
        }

        $this->forceFill([
            'current_team_id' => $team->id,
        ])->save();

        $this->setRelation('currentTeam', $team);

        return true;
    }
  1. In AppLayout.vue to links route('teams.show', $page.props.user.current_team) add the condition v-if="$page.props.user.current_team". You should get something like the following:
<!-- Team Settings -->
<jet-dropdown-link
  v-if="$page.props.user.current_team"
  :href="route('teams.show', $page.props.user.current_team)"
>
  Team Settings
</jet-dropdown-link>

and

<!-- Team Settings -->
<jet-responsive-nav-link
  v-if="$page.props.user.current_team"
  :href="route('teams.show', $page.props.user.current_team)"
  :active="route().current('teams.show')"
>
  Team Settings
</jet-responsive-nav-link>

Perhaps I have not considered something else.
I think the rest of the logic can be easily implemented.

@kieferjs
Copy link

kieferjs commented Mar 6, 2021

I brought the teams into my laravel project but I am using this for agents and teams, not the users table. I updated the jetstream class file for the $userModel to reference 'App\Models\Agent" and changed the foreign_key to agent_id. The issue I have is when the HasTeams trait function pivots on the team_agent pivot table. It keeps looking for the team_user table. When I renamed the pivot table to team_user, it gets tripped up on my tenant_id in the where clause. That is a column in the pivot table for my multi-tenancy. Is there another way to configure the code to use the new pivot table name?

@VKambulov
Copy link
Contributor

I brought the teams into my laravel project but I am using this for agents and teams, not the users table. I updated the jetstream class file for the $userModel to reference 'App\Models\Agent" and changed the foreign_key to agent_id. The issue I have is when the HasTeams trait function pivots on the team_agent pivot table. It keeps looking for the team_user table. When I renamed the pivot table to team_user, it gets tripped up on my tenant_id in the where clause. That is a column in the pivot table for my multi-tenancy. Is there another way to configure the code to use the new pivot table name?

You may try to add $table variable with your new pivot table name in Membership model class, like at:

/**
 * The table associated with the pivot model.
 *
 * @var string
 */
protected $table = 'team_user';

If you not have Membership model in your project, you may extend this class from Laravel\Jetstream\Membership, like at:

<?php

namespace App\Models;

use Laravel\Jetstream\Membership as JetstreamMembership;

class Membership extends JetstreamMembership
{
    /**
     * The table associated with the pivot model.
     *
     * @var string
     */
    protected $table = 'team_agent';
}

It's hard for me to understand without specific examples, but I hope I understood you correctly.

@kieferjs
Copy link

kieferjs commented Mar 6, 2021

I brought the teams into my laravel project but I am using this for agents and teams, not the users table. I updated the jetstream class file for the $userModel to reference 'App\Models\Agent" and changed the foreign_key to agent_id. The issue I have is when the HasTeams trait function pivots on the team_agent pivot table. It keeps looking for the team_user table. When I renamed the pivot table to team_user, it gets tripped up on my tenant_id in the where clause. That is a column in the pivot table for my multi-tenancy. Is there another way to configure the code to use the new pivot table name?

You may try to add $table variable with your new pivot table name in Membership model class, like at:

/**
 * The table associated with the pivot model.
 *
 * @var string
 */
protected $table = 'team_user';

If you not have Membership model in your project, you may extend this class from Laravel\Jetstream\Membership, like at:

<?php

namespace App\Models;

use Laravel\Jetstream\Membership as JetstreamMembership;

class Membership extends JetstreamMembership
{
    /**
     * The table associated with the pivot model.
     *
     * @var string
     */
    protected $table = 'team_agent';
}

It's hard for me to understand without specific examples, but I hope I understood you correctly.

That resolved my issue. Thanks!

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

8 participants