Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Michael Fayez's avatar

what is the problem In this code ?

when I create a new team, the data saved as a admin team !! but when I make edit the data saved exactly as I inserted !! Here is the Livewire Create :-

<?php

namespace App\Http\Livewire\Team;

use App\Models\Team;
use App\Models\User;
use Livewire\Component;

class Create extends Component
{
    public Team $team;

    public array $listsForFields = [];

    public function mount(Team $team)
    {
        $this->team = $team;
        $this->initListsForFields();
    }

    public function render()
    {
        return view('livewire.team.create');
    }

    public function submit()
    {
        $this->validate();

        $this->team->save();

        return redirect()->route('admin.teams.index');
    }

    protected function rules(): array
    {
        return [
            'team.name' => [
                'string',
                'required',
            ],
            'team.owner_id' => [
                'integer',
                'exists:users,id',
                'required',
            ],
        ];
    }

    protected function initListsForFields(): void
    {
        $this->listsForFields['owner'] = User::pluck('name', 'id')->toArray();
    }
}

Here Is the Livewire Edit :-

<?php

namespace App\Http\Livewire\Team;

use App\Models\Team;
use App\Models\User;
use Livewire\Component;

class Edit extends Component
{
    public Team $team;

    public array $listsForFields = [];

    public function mount(Team $team)
    {
        $this->team = $team;
        $this->initListsForFields();
    }

    public function render()
    {
        return view('livewire.team.edit');
    }

    public function submit()
    {
        $this->validate();

        $this->team->save();

        return redirect()->route('admin.teams.index');
    }

    protected function rules(): array
    {
        return [
            'team.name' => [
                'string',
                'required',
            ],
            'team.owner_id' => [
                'integer',
                'exists:users,id',
                'required',
            ],
        ];
    }

    protected function initListsForFields(): void
    {
        $this->listsForFields['owner'] = User::pluck('name', 'id')->toArray();
    }
}

what's missing ??

0 likes
15 replies
LaryAI's avatar
Level 58

The code provided is missing the logic to differentiate between creating a new team and editing an existing team. Currently, both the Create and Edit components have the same logic for saving the team data, which is causing the issue where the data is always saved as a new team.

To fix this issue, you need to modify the submit method in the Edit component to update the existing team instead of creating a new one. You can achieve this by using the update method instead of the save method.

Here's the updated code for the Edit component:

<?php

namespace App\Http\Livewire\Team;

use App\Models\Team;
use App\Models\User;
use Livewire\Component;

class Edit extends Component
{
    public Team $team;

    public array $listsForFields = [];

    public function mount(Team $team)
    {
        $this->team = $team;
        $this->initListsForFields();
    }

    public function render()
    {
        return view('livewire.team.edit');
    }

    public function submit()
    {
        $this->validate();

        $this->team->update();

        return redirect()->route('admin.teams.index');
    }

    protected function rules(): array
    {
        return [
            'team.name' => [
                'string',
                'required',
            ],
            'team.owner_id' => [
                'integer',
                'exists:users,id',
                'required',
            ],
        ];
    }

    protected function initListsForFields(): void
    {
        $this->listsForFields['owner'] = User::pluck('name', 'id')->toArray();
    }
}

With this change, the Edit component will now update the existing team instead of creating a new one when the form is submitted.

Snapey's avatar

what determines an "admin team"

Michael Fayez's avatar

@Snapey I don't know but when I save the project and add the team .. it saved as admin team but when I make edit it saved as exactly as i inserted the data here is the project create code ...

<?php

namespace App\Http\Livewire\Project;

use App\Events\UsersAssignedToNewProject;
use App\Models\Package;
use App\Models\Project;
use App\Models\Team;
use App\Models\User;
use Livewire\Component;

class Create extends Component
{
    public Project $project;

    public array $package = [];

    public array $assignees = [];

    public array $listsForFields = [];

    public function mount(Project $project)
    {
        $this->project = $project;
        $this->initListsForFields();
    }

    public function render()
    {
        return view('livewire.project.create');
    }

    public function submit()
    {
        $this->validate();

        $this->project->save();
        $this->project->package()->sync($this->package);
        $this->project->assignees()->sync($this->assignees);

        event(new UsersAssignedToNewProject($this->project));

        return redirect()->route('admin.projects.index');
    }

    protected function rules(): array
    {
        return [
            'project.name' => [
                'string',
                'required',
                'unique:projects,name',
            ],
            'project.owner_id' => [
                'integer',
                'exists:users,id',
                'required',
            ],
            'package' => [
                'required',
                'array',
            ],
            'package.*.id' => [
                'integer',
                'exists:packages,id',
            ],
            'project.start_date' => [
                'required',
                'date_format:' . config('project.date_format'),
            ],
            'project.end_date' => [
                'required',
                'date_format:' . config('project.date_format'),
            ],
            'project.statues' => [
                'nullable',
                'in:' . implode(',', array_keys($this->listsForFields['statues'])),
            ],
            'assignees' => [
                'required',
                'array',
            ],
            'assignees.*.id' => [
                'integer',
                'exists:users,id',
            ],
            'project.team_id' => [
                'integer',
                'exists:teams,id',
                'required',
            ],
        ];
    }

    protected function initListsForFields(): void
    {
        $this->listsForFields['owner']     = User::pluck('name', 'id')->toArray();
        $this->listsForFields['package']   = Package::pluck('name', 'id')->toArray();
        $this->listsForFields['statues']   = $this->project::STATUES_RADIO;
        $this->listsForFields['assignees'] = User::pluck('email', 'id')->toArray();
        $this->listsForFields['team']      = Team::pluck('name', 'id')->toArray();
    }
}

also you can clone the repo and see the issue... by the way do you remember from one week when I was searching in how to make observer send email to the assignees I was wrong it needs to be event and listener .. it's a great experience.. https://github.com/mc0de/laratopia-help

Snapey's avatar

You have basically repeated what you said before. I still have no clue what an 'admin team' is and how you determine if one was or was not created.

This;

when I create a new team, the data saved as a admin team !! but when I make edit the data saved exactly as I inserted !!

does not make ANY sense to someone not involved in the project

Michael Fayez's avatar

@Snapey it's database seeder for teams as admin's team It will be clear if you clone repository and try it yourself If Laracasts support video I should've make a video to show it to you ... Really I wonder what's missing !!

Michael Fayez's avatar

@Snapey when I save any new data in the project model, it saved as admin’s team ! Not any other team although we choose another team .

Snapey's avatar

@Michael Fayez the code you show is creating a team . Nothing to do with the question then ?

Michael Fayez's avatar

@Snapey This is the Project Create :-

<?php

namespace App\Http\Livewire\Project;

use App\Events\UsersAssignedToNewProject;
use App\Models\Package;
use App\Models\Project;
use App\Models\Team;
use App\Models\User;
use Livewire\Component;

class Create extends Component
{
    public Project $project;

    public array $package = [];

    public array $assignees = [];

    public array $listsForFields = [];

    public function mount(Project $project)
    {
        $this->project = $project;
        $this->initListsForFields();
    }

    public function render()
    {
        return view('livewire.project.create');
    }

    public function submit()
    {
        $this->validate();

        $this->project->save();
        $this->project->package()->sync($this->package);
        $this->project->assignees()->sync($this->assignees);
        $this->dispatchBrowserEvent('swal:modal', [
            'type' => 'success',
            'title' => 'Project added successfully',
            'text' => '',
        ]);

        event(new UsersAssignedToNewProject($this->project));

        return redirect()->route('admin.projects.index');
    }

    protected function rules(): array
    {
        return [
            'project.name' => [
                'string',
                'required',
                'unique:projects,name',
            ],
            'project.owner_id' => [
                'integer',
                'exists:users,id',
                'required',
            ],
            'package' => [
                'required',
                'array',
            ],
            'package.*.id' => [
                'integer',
                'exists:packages,id',
            ],
            'project.start_date' => [
                'required',
                'date_format:' . config('project.date_format'),
            ],
            'project.end_date' => [
                'required',
                'date_format:' . config('project.date_format'),
            ],
            'project.statues' => [
                'nullable',
                'in:' . implode(',', array_keys($this->listsForFields['statues'])),
            ],
            'assignees' => [
                'required',
                'array',
            ],
            'assignees.*.id' => [
                'integer',
                'exists:users,id',
            ],
            'project.team_id' => [
                'integer',
                'exists:teams,id',
                'required',
            ],
        ];
    }

    protected function initListsForFields(): void
    {
        $this->listsForFields['owner']     = User::pluck('name', 'id')->toArray();
        $this->listsForFields['package']   = Package::pluck('name', 'id')->toArray();
        $this->listsForFields['statues']   = $this->project::STATUES_RADIO;
        $this->listsForFields['assignees'] = User::pluck('email', 'id')->toArray();
        $this->listsForFields['team']      = Team::pluck('name', 'id')->toArray();
    }
}

when I save team's name it save the default which make me make edit and when I make edit the team saved successfully so what is the problem please !!

Snapey's avatar

nope, still don't understand what you are asking

Snapey's avatar

@Michael Fayez something wrong in the HasTeam trait on the user model.

When the team is null, it sets it to 1 as part of the model save

tangtang's avatar
tangtang
Best Answer
Level 6

check this code again from the app/Traits/Tenantable.php

static::creating(function (Model $model) {
            if (!auth()->check()) {
                return;
            }

            $model->team_id = auth()->user()->is_team_owner ? auth()->user()->ownedTeam->id : auth()->user()->team_id;
        });

is this code is required ? seems like it will lock the team_id for every new data

$model->team_id = auth()->user()->is_team_owner ? auth()->user()->ownedTeam->id : auth()->user()->team_id;

you can delete it and try to add new data.

you can compare it for edit (just in case).

try to change creating to updating. this will be reverse version from this odd behavior team_id in your case

make edit will save as you like .. this is the tricky issue I have more than one week in this issue
static::updating(function (Model $model) {
            if (!auth()->check()) {
                return;
            }

            $model->team_id = auth()->user()->is_team_owner ? auth()->user()->ownedTeam->id : auth()->user()->team_id;
        });

using this code will lock the team_id value every updated data.

you can delete this code or find another way to handle the logic for static::creating

1 like

Please or to participate in this conversation.