Generating Relation Manager0:00
So the last thing we need when we're viewing our speaker to make this page complete is to be able to see what different talks that this speaker has submitted, and maybe have they been approved or not, different things like that. And so in order to do that, Filament gives us a really powerful thing. This is one of my favorite things about the Filament panel. And just to be clear, this is only available in the Filament panel. However, if you needed this functionality outside of it, you could just create your own Filament table and insert it into whatever page you're inserting it into. So we're going to go into our terminal and create a FilamentRelationManager. So we're just going to do php artisan make:filament-relation-manager, and we'll fill in the rest.
So we're going to go into our terminal and create a Filament Relation Manager. So we're just going to do php artisan make:filament:relation-manager, and we'll fill in the rest with the prompts. So what resource would you like to create this in? We are doing a Speaker resource. The relationship is talks, and the title attribute for a talk is actually title. And so there we go, and it does tell us, make sure we register it in the Speaker resource getRelations. So we go into our editor, and we go to the Speaker resource, and we have this getRelations method that we have been ignoring the entire course.
Registering Speaker Relations1:02
So we go into our editor, and we go to the Speaker resource, and we have this getRelations method that we have been ignoring the entire course. It's finally time to start using it. And we're going to have the TalksRelationManager class, and we're going to pass this in. And then let's go to the browser and see what that did. I refresh the page, and now you can see I've got talks here. So this User has submitted two different talks, and right now it's just a table with one column and nothing editable or anything like that, so we can go change that now. So we go back here, and we open up the RelationManager. We can see the relationship is talks, and we've got a couple things to find here.
So we go back here, and we open up the Relation Manager. We can see the relationship is talks, and we've got a couple things to find here. We have a form, and we have the table. Now this is very similar to the resource, and in some cases you might find that what you've already built for talks, for example, like the talk form, I think we can probably reuse that here, and it's going to make a lot of sense. We do need to make some small changes. For the table, it's possible that the same fields you want on your talks table are the same that you would want here, but usually that's not the case because our talks table has a column for the speaker, and it has a column for the picture of who the speaker
Reusing Talk Form2:05
same that you would want here, but usually that's not the case because our talks table has a column for the speaker, and it has a column for the picture of who the speaker is. But when you're looking at all the talks on a speaker itself, those columns don't really make sense. So we typically like to use the Relation Manager columns and the table itself and make it kind of its own custom thing that's very specific to the page that it lives on. So let's go ahead and implement this stuff now. We'll start with the form. We can still share the talk form, so let's get the Talk model and get form.
We'll start with the form. We can still share the talk form, so let's get the Talk model and get getForm. It looks like we did not extract that, so let's go to the TalkResource, and here's the form that we're using, so let's see what we've done before. Extract that, and we're going to go to the Talk model and add the getForm, and import our classes. This one, let's change to a rich editor. Okay, so now we've got the talk form, and we're sharing it, but the thing is speakerId. If I'm creating a talk on a speaker page, I don't want to select the speakerId.
Enabling View Page Editing3:09
ID. If I'm creating a talk on a speaker page, I don't want to select the speaker ID. That doesn't make any sense. We need to have a way to hide this when it is on the Relation Manager. One more thing is when we go to the browser and we take a look, there is no button here to create a new talk, so we need to fix that first, and the reason for that is because in the Relation Manager, we need to add a method here for isReadOnly, and sometimes you're going to want your Relation Manager to be read-only. By default, it is read-only on a view page, and it is editable on an edit page, but I like it to be editable on the view page as well, so I'm just going to return false here.
By default, it is read-only on a view page, and it is editable on an edit page, but I like it to be editable on the view page as well, so I'm just going to return false here, and now if we go back to the browser, we can see that we are able to add a new talk, edit, or delete this talk. So when we go to new talk, now here where you can see, you have to select the speaker, but we already know who the speaker is, so that doesn't make a lot of sense. Let's find a way to use the same form and not have duplication, but also make sure it's hidden when it needs to be, and the way that we do that, again, we're in the RelationManager. Let's just pass in the speakerId here, and if there is a speakerId, then we're just going to hide it, because the RelationManager is going to handle making sure to associate.
Let's just pass in the speakerId here, and if there is a speakerId, then we're just going to hide it, because the RelationManager is going to handle making sure to associate the talk that was created on that speaker page with that speaker. So in order to get the record, the owner record, there's a method where we just say getOwnerRecord, and so that's just the record that the RelationManager lives on. In this case, it is the speaker, and let's just get the ID, and then when we go to the getForm, let's just pass in a speakerId, and we'll default it to null. So our other form where we're using this, it will always still work, but if we do pass in a speakerId, then let's just have here, we'll have this hidden. We need to pass in that speakerId, and we'll just return when the speakerId is not null,
in a speakerID, then let's just have here, we'll have this hidden. We need to pass in that speakerID, and we'll just return when the speakerID is not null, then we're going to hide this input. So if we go back to the browser, let's go ahead and click add a new talk, and now we just have the title and the abstract, so some new talk here, and we have whatever our abstract, and if we create that, now we have the talk, and it's associated to the speaker. Now as I've said in other videos, when I am doing something, especially filling out a form, I don't like it as a modal, I really like that as a slideover, and I've had to continually add that slideover method and chain it to the different actions, I don't want to do that anymore, so there is a way to make that a little more simple, and to
Setting Global Slideover Defaults5:30
continually add that slideover method and chain it to the different actions, I don't want to do that anymore, so there is a way to make that a little more simple, and to do that, we need to go into any AppServiceProvider, I just like to use the one that comes standard with Laravel, and in the boot method, we have the ability to actually set global defaults. So let's go back to our RelationManager and take a look in the table, this is the tables.create action. So what I want to do is actually get this action itself, go back to my AppServiceProvider, paste it in here, I'll actually import the namespace, but I want to make sure it's the tables.actions, there we go, and I'm going to call a static method called configureUsing,
paste it in here, I'll actually import the namespace, but I want to make sure it's the tables actions, there we go, and I'm going to call a static method called configure using, and what this is going to allow me to do is pass in the action, and then set some defaults that I like to use always, so in this case it's really simple, I'll just return action slideover, and that's it. Every time Laravel boots up, it's going to run through here, and by default, every create action on a table is going to use a slideover. Now I could override it, if for some reason I want to use the modal on that particular create action, but if we go back to the browser here, and we do a new talk, now we're going to get a slideover every time.
Customizing Columns and Tabs6:36
create action, but if we go back to the browser here, and we do a new Talk, now we're going to get a slideover every time. That is useful for a bunch of stuff, if we wanted to configure which of these different things in the rich editor we want to see, you don't want to have to do that every time you add a rich editor, so you do that in the AppServiceProvider, and it will be consistent across your entire app. So now let's close this out, and this is just a normal table, so all the stuff we did with other tables, if we go back into the RelationManager, we can add whatever columns we want, so if we need the status here, we can add that in, and it shows up, everything else that we did, now as I mentioned, we don't want all the fields from the other Talk table,
so if we need the status here, we can add that in, and it shows up, everything else that we did, now as I mentioned, we don't want all the fields from the other talk table, if we go look at that right now, it doesn't make sense to have a speaker avatar, it doesn't make sense to show the speaker, some of these things we could put in there, I'll leave that for you to do yourself, but this is just an incredible way to make it really easy for you to handle related records on the record itself. Now lastly, as you're building your app, you're probably going to end up with a lot of different relation managers on the same record, and Filament allows you to do that really well, so if we go back to our speaker resource, now I'm just going to add a duplicate of this relation manager, just so that you can see it, but when I add that here and I go back
so if we go back to our Speaker resource, now I'm just going to add a duplicate of this relation manager, just so that you can see it, but when I add that here and I go back to the browser, here, now it's not actually going to work because it's a duplicate, but when you have different models, you're going to be able to switch back and forth, you can even customize the icon and how these look, so when you have multiple relationships for a single model, you can add your relation managers here, and you can interact with not just the model, but all its relationships in one place, it's super powerful.
