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

elbsurfer's avatar

How to save the current team's id?

Hello, I want to enhance Spark by adding a custom form where users can input data which is saved to the database table that I have created with a migration.

I want to have events which belong to a team and therefore I created this database:

        Schema::create('events', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->index();
            $table->integer('team_id')->index();
            $table->string('event_name');
            $table->dateTime('event_datetime');
            $table->integer('location_id');
            $table->integer('max_pl');
            $table->integer('min_pl');
            $table->boolean('waitinglist');
            $table->integer('comment');
            $table->integer('kind_of');
            $table->timestamps();
        });

I save the contents with the following method:

        return $request->user()->events()->create([
            'event_name' => $request->event_opponent,
'team_id' => ...?

The user_id gets automatically filled thanks to $request->user() but what is the best way now to save the current's team id with Laravel/Spark in the database here?

0 likes
12 replies
dannydjones's avatar
Level 6

From the Spark docs:

You may access the user's "current team" using the currentTeam property on a App\User instance:

echo $user->currentTeam->name;

https://spark.laravel.com/docs/1.0/teams

so you could do:

'team_id' => $request->user()->currentTeam->id;

I hope this helps :)

EventFellows's avatar

There is also a current_team_id field on the users table that you can use directly.

1 like
elbsurfer's avatar

EventFollows, how does that work. Do you have an example?

Is it this?

$request->user()->current_team_id

That would be great. Thanks!

EventFellows's avatar

Yep, that should be it. For the ID you can use it on any user instance you have. If you need other team related values go with the suggestion from @dannydjones1994 above.

elbsurfer's avatar

EventFollows, thanks. That worked.

I am struggling a bit, because it is a lot more different to Laravel 4 which I have used before.

For example: If I want to query all events I have created now, how can I achieve that?

In the Team Model I created:

    public function events()
    {
        return $this->hasMany(Event::class)->orderBy('created_at', 'asc');
    }

The same in the User-Model, because I have a team_id and a user_id in the database for each event.

But how can I get all Events for one team now - the following is not working:

    public function all(Request $request)
    {
        return $request->team()->events;
    }

Am I missing something? I think the team is not known to $request?

EventFellows's avatar

What does this output? If it not a team instance it cannot work, if it is it should work.

    public function all(Request $request)
    {
        dd($request->team())
    }

Your approach in general is right, though.

this will work if you have a team instance available:

    public function teamEvents()
    {
        $team Team::findOrFail('put any valid team id here');
        return $team->events;
    }
elbsurfer's avatar

I added:

    public function all(Request $request)
    {
        dd($request->team());
    }

And I get a 500 error in my App.js and it shows:

BadMethodCallException in Macroable.php line 81:
Method team does not exist

in Macroable.php line 81
at Request->__call('team', array()) in EventController.php line 30
at EventController->all(object(Request))
EventFellows's avatar

You need to get a team instance. If your request does not have one, you can either get it like this (I think spark has is out of the box):

$user->currentTeam()

Or you make sure there is a team instance on the request itself.

So if request has a user() this should work like this $request->user()->currentTeam()->events;

But of course this depends on your logic whether events in its core belong to the user or the team.

elbsurfer's avatar

That worked. Thanks.

But just out of curiousity: How can I attach a team instance to the request?

My Event Model is set up as:

    /**
     * Get the user that created the event.
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * Get the team that belongs to the event
     */
    public function team()
    {
        return $this->belongsTo(Team::class, 'team_id');
    }

So it belongs to both...

blackowl's avatar

Hello guys,

Sorry for the dumb questiont, the following give me an error:

$team_id = $user->currentTeam->id;

error:

 ErrorException (E_NOTICE)
Undefined variable: user

What I'm doing wrong? Thank you!

aullah's avatar

Hi blackowl,

I just stumbled across this thread and your post. I'm replying for anyone who finds this thread in the future too (or if you still have the problem, I'd be happy to assist).

Your error of the undefined variable user is occurring because $user is not defined within the scope you're calling it. For us to investigate your error we'd need to know what class, method and/or context you're using it within. Seeing the rest of your code would be required. :)

Please or to participate in this conversation.