laravel / jetstream

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

Error when user dont have any teams #188

Closed youyi1314 closed 4 years ago

youyi1314 commented 4 years ago

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

Transferred to the jetstream repo.

taylorotwell commented 4 years ago

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

youyi1314 commented 4 years ago

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

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

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

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

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

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

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;
    }
  2. 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 commented 3 years ago

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

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

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!