Current Team Concept0:00
Hi, this is Taylor Rottweil, back with another Laravel screencast. And in the previous screencast, I kind of gave you an introduction to team billing. And in this screencast, I want to talk a little bit more about this concept of the current team. And I want to show you how you can interact with that in your JavaScript and in your PHP code. Now, let's say you're building a to-do list application, and users of this application can belong to multiple teams. And when they switch teams, we want to show the to-do list for that team only. So, for example, when I switch to the Laracast team, I want this dashboard to show the to-do list for the Laracast team. And when I'm viewing the Laravel team, I want it to show the to-do list for the Laravel team.
I want this dashboard to show the to-do list for the Laracast team. And when I'm viewing the Laravel team, I want it to show the to-do list for the Laravel team. Now, we need a way to access what team the user is currently on, both in our JavaScript and in our PHP. And now this is controlled by a currentTeamId flag that actually sits on the user's database table. But let's take a look at how we can access this. I want to go ahead and pull up my developer console here so that we can take a look at a few things as we go. All right, so let's pull up our code. And if you remember from one of our previous screencasts,
Reading Team in JavaScript1:03
All right, so let's pull up our code. And if you remember from one of our previous screencasts, the home screen or the home template, which is just our default dashboard, which you can change to whatever you like, has a home.js that goes with it. Now, within this piece of code, we can actually access the current team. So, within this ready method, let's just dump out something. Let's dump out console.log(SparkState.currentTeam.name). All right, let me compile this JavaScript. Now, what this does is this SparkState object has several things available to you.
All right, let me compile this JavaScript. Now, what this does is this SparkState object has several things available to you. It gives you the current user, it gives you the current teams that the user belongs to, all of the teams as an array, and it also gives you the current individual team that the user is viewing. So, for right now, let's just make sure that we can dump out the current team name. All right, so our JavaScript is compiled. And if we hit this route again, we can see that we're currently viewing this Laravel team.
And if we hit this route again, we can see that we're currently viewing this Laravel team. And that corresponds with the team you can see is selected here. All right, so what that lets you do is when I, if I need to get the current ID for the team, I can just get it just like that, spark.state.currentTeam.id. And if I need to send that in any AJAX request up to the server, that's how I know what team ID to send. All right, so let's go ahead and try to pull something from the server for the current team, not using JavaScript to access the ID.
Accessing Team in PHP2:51
All right, so we're going to just call this route and dump out what we get back. Now let's define this on the back end so we can see how do we use the current team on the back end in our php code? All right, so let's define this teamName route. And now right here, let's do return auth()->user->currentTeam->name. All right, so at any time we have a user instance, we can just access the currentTeam property. And that's going to reflect the current team the user is viewing. All right, so now if we call this, if we compile our JavaScript and call this URL, we should just get back in our console the same thing, Laravel as the team name.
All right, so now if we call this, if we compile our JavaScript and call this URL, we should just get back in our console the same thing, Laravel as the team name. And this just demonstrates how you can access the current team from your back end. If you need to constrain your queries, for example, to only pull the to-do list for the current team ID, this is how you would access that information. All right, so let's hit this URL. All right, and there's Laravel right there. And that actually pulled it from the back end. So that wasn't dumped out by accessing the current team object in JavaScript.
Verifying by Switching Teams3:51
And that actually pulled it from the back end. So that wasn't dumped out by accessing the current team object in JavaScript. That actually made an AJAX call off to our back end to the team name route and got Laravel back. So that's just a quick demonstration of how you can access the current team in your PHP code. Now let's jump back into our web browser and switch teams. When we switch teams, remember, it's still going to make an AJAX request to this route. But when we switch teams, it should be a different team name coming back. So when we switch to the Laracast team, this route should be returning Laracast and dumping that out in our JavaScript console.
So when we switch to the Laracast team, this route should be returning Laracast and dumping that out in our JavaScript console. Let's take a look at that. All right, so we're still on the Laravel team, as you can see here. Now let's switch to Laracast. All right, and you can see it dumped out Laracast as we would expect. So that means it made the AJAX call to this team name route and got back Laracast from our php. And if we switch back to Laravel, as you would expect, we get Laravel. So that's just a quick introduction and overview of how you can interact with the current team.
And if we switch back to Laravel, as you would expect, we get Laravel. So that's just a quick introduction and overview of how you can interact with the current team that a User is viewing in your Laravel Spark application.
