Trouble with Attach (Detach works)

I'm new to Filament, so this may be obvious, but I've got a simple many-to-many relationship set up and working. It's a "Family" that can have one or more "Person", and a "Person" can belong to more than one "Family". Pardon the funky plurals here. I'm still trying to figure out how Laravel likes these to be done. My issue is that when I go to Attach a record to a Person who belongs to a Family, the Attach box comes up, but the pulldown is blank. However, the Search window can be typed in, and it pulls up real data. I can pick from the data (People) and attach the record. See enclosed pic. The tables are tiny (about 50 rows of fake data. So all of this kinda works, but there are two issues: 1. The pulldown doesn't populate, so I'm forced to use search. 2. The Search only shows the first_name field and I'd like it to show the last_name field as well. I've googled and googled and exhausted myself on ChatGPT, but I can't find a simple explanation of "put this code in these files, and it will work". So many examples show code, but don't reference exactly what file to put it in. The Laracast course goes very fast and barely touches on this topic. Can anyone point me to a newbie HowTo on this subject? I'm soooo close to getting this working, but the solution is elusive.
No description
39 Replies
Vp
Vp3mo ago
put some code here as well otherwise no one will be able to help you My guess: 1. you need to use ->preloadRecordSelect() 2. you need to override how data is displayed (or use virtual column)
Stricks
StricksOP3mo ago
@Vp - here goes... First off, I'm running reasonably current verisons of things. PHP 8.2, Node 22, and the latest versions of Laravel and Filament via composer. I'm on a Mac using Herd Pro. I've enclosed an image of my dir structure so show you I think I have the files in the right place. I'm open to any and all suggestions. I am just testing code, so if you tell me to wipe and start again, that's fine. Many thanks on any suggestions! -- Stricks
No description
Stricks
StricksOP3mo ago
Sample code enclosed
Vp
Vp3mo ago
Can't you just send the related code only.. I am not going to download to check.. And did you even try my guess (suggestions)
Stricks
StricksOP3mo ago
Sorry @Vp - I didn't see your reply until this evening. I will look into that ASAP. @Vp : I've tried ->preloadRecordSelect() adding it to this section, but it didn't make a difference.
->headerActions([
Tables\Actions\CreateAction::make(), // Header Create
Tables\Actions\AttachAction::make() // Header Attach many-to-many
->label("Attach Existing Person") // Label above People table.
->preloadRecordSelect()
->headerActions([
Tables\Actions\CreateAction::make(), // Header Create
Tables\Actions\AttachAction::make() // Header Attach many-to-many
->label("Attach Existing Person") // Label above People table.
->preloadRecordSelect()
Remember, I don't understand the underlying code of Filament or AWCodes's module, so this is a black box to me. I want to get it working first, and then start looking at the underlying code to understand it. I'm happy to post "related code", but I don't know what part of the code that means. If you could give me a PHP filename and an function, I'd be happy to post anything you'd like to see. Remember I have not written any of my own code yet. All the functions have been created by Filament or created with the Make command, so amy filenames or functions you reference will be the standard ones. Thanks! Additional info: I've created another relationship by using the CLI commands to create a model Property (Factory, Seeder, etc., and Family<->Property is many-to-many), and then the Relationship with Filament/Resources/FamilyResource/RelationManagers/PersonRelationManager.php but I am getting the same Attach issues as before.
So clearly, I'm making the same mistake. I doubt this is a typo.
awcodes
awcodes3mo ago
For starters the inverse relationship name needs to be the actual name of the model’s relationship, so ‘family’ not ‘Family’. But that’s just one thing I noticed in the code you shared. There code by some other things going on too.
awcodes
awcodes3mo ago
Maybe also check out https://m.youtube.com/@FilamentDaily if the other resources you referenced are moving too fast for you.
YouTube
Filament Daily
Quick tips and tricks about the Laravel Filament system.
awcodes
awcodes3mo ago
But as long as the relationships are all setup correctly most of it should just work out of the box with the relationship managers.
Stricks
StricksOP3mo ago
Thanks @awcodes - I'll watch it now. @awcodes May sound crazy but... Is it possible I have had this working the whole time but I don't understand how the Attach UI works. When I do an Attach, I see the modal, but the field (in my case first_name) is blank. Since there are my people in the database, I would assume it would show a pulldown of names and also let me search. The search works fine, but the pulldown is empty. Does the UI show an empy box, and you must search for the entry to pull it up? IOW, do you never see a first_name until you search for it?
awcodes
awcodes3mo ago
But I’d have to see a lot more code to offer any real insight. If you can share the codebase as a GitHub repo it would be easier for us trying to help to get a more full picture of what you have tried or are trying to do.
Stricks
StricksOP3mo ago
Excellent idea on sharing via GitHub. I will work on a (relatively) clean build and get it up there. In the mean time I'll check out that link.
awcodes
awcodes3mo ago
If the search works then the relationship is fine. You just have to tell it to preload because filament doesn’t want to make assumptions and extra db queries if it doesn’t have to.
Stricks
StricksOP3mo ago
And is the ->preload() appended to the Filament/Resources/FamilyResource/RelationManagers/FamilyRelationManager.php file, where "Family" is half of the Pivot table? It would seem a logical choice, but your reference to the docs above is on a Select and this is on the Table function and is thus full of Columns. I haven't dug enough into the Filament code to find what creates the modal where the user is allowed to search.
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make()
->preload(),
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make()
->preload(),
])
Oh, and I should add the above code doesn't populate the pulldown. I still have to use the search. Sorry to be such a pain about this small UI bit, but I feel I am making a mistake that will follow me going forward if I don't learn how to fix it now.
awcodes
awcodes3mo ago
Preload is specific to the form field, in the case of the docs it’s the select field. The attach action in this case loads a modal with its own form. So in that form you have a field that determines which record to attach. So that form typically has a select field to find a record to attach. I understand the scoping of functionality can be confusing sometimes and the docs can feel disjointed. Just hang in there. I promise once you start to understand it and it clicks it will get better. Try this on the action ->preloadRecordSelect()
Stricks
StricksOP3mo ago
Here's the GitHub Public repo. Pardon the sloppy code. I only did a quick clean.
Try this on the action ->preloadRecordSelect()
Will do !
awcodes
awcodes3mo ago
So AttachAction::make()->preloadRecordSelect()
Stricks
StricksOP3mo ago
Nope - still the same. I think I've got something set deep in the code that must be turing this off. I've even wiped the project and started from scratch to see if I could avoid this.
No description
awcodes
awcodes3mo ago
Hmm, leads me to think the relationship is off somewhere. Diving the code I would expect that to work if the relationship is set up correctly.
Stricks
StricksOP3mo ago
AttachAction::make()->preloadRecordSelect()
Just to be clear, the above is on the headerActions section - right?
awcodes
awcodes3mo ago
Yea
MilenKo
MilenKo3mo ago
->preloadRecordSelect() method is used to pre-load all selector options and not to populate a selected already and stored in DB record (if I understood correctly that is what was needed)
Stricks
StricksOP3mo ago
Yes, the records are already in a 'Person' MySQL table.
awcodes
awcodes3mo ago
Well, yea if it’s already associated then it wouldn’t be available to be associated again.
MilenKo
MilenKo3mo ago
Any log errors? Or console debug output?
awcodes
awcodes3mo ago
Associate is to add new associations not mange the associations that’s what the table/row actions are for.
MilenKo
MilenKo3mo ago
From what I know, Filament is smart enough to know the input field based on the column relation of the make() method, so if any record is stored in the DB matching the column name, filament should set it as a default and pre-populate as a selected option. That is why I aggree with @awcodes that there is something fishy with your relations... Check your relations using dd() or \Log() I mean dd the output you should get from the specific column and see if it even returns a value. Another thing can be if the model does not have the rifht to store the object data (guarder or filable properties are not set on the model)
awcodes
awcodes3mo ago
@Stricks feel free to share a repo and I’ll look into it further.
MilenKo
MilenKo3mo ago
I aggree more code is needed (unless it is prprietary) but otherwise there are only many GUESSES 🙂
Stricks
StricksOP3mo ago
GitHub
GitHub - dstrickler/nhadbv2-filament
Contribute to dstrickler/nhadbv2-filament development by creating an account on GitHub.
Stricks
StricksOP3mo ago
Many, many thanks for all the help on this!
Stricks
StricksOP3mo ago
WOW! OK, testing
awcodes
awcodes3mo ago
The relationship on the model is ‘family()’ so the name of the relationship is ‘family’ not ‘Family’ And the same for the person on the inverse. Ie ‘streetAdress’ is not the same as ‘StreetAddress’ as an example. Relationships in laravel are inferred through reflection so the casing matters.
Stricks
StricksOP3mo ago
protected static string $relationship = 'Family';
So I changed the above to a lower-case 'family', but it still doesn't work. It sounds like I need to go through all the code and make sure I have the case right. Is there a document that says what the correct case should be? I have seen (I think) conflicting information. Perhaps it only matters in certain places?
awcodes
awcodes3mo ago
Filament defaults to laravel for all of this. So, maybe spend a few minutes going through the laravel docs of how the relationships work to get a better understanding of that first.
Stricks
StricksOP3mo ago
Agreed. Excellent advice. Thank you!
awcodes
awcodes3mo ago
If you’re still stuck after that just let us know.
Stricks
StricksOP3mo ago
I have the pulldown working (yes!), but I don't know what caused the fix yet. I did have to add some code that doesn't make sense to me. In the model "family()", I had to define both "person()" and "people()" functions like below. This indicates other issues in the code - correct? I would assume one function should be enough - and it should be singular as the models family() and person() are both singular.

public function person()
{
return $this->belongsToMany(Person::class);
}

public function people()
{
return $this->belongsToMany(Person::class);
}

public function person()
{
return $this->belongsToMany(Person::class);
}

public function people()
{
return $this->belongsToMany(Person::class);
}
@awcodes I can confirm this helped with the pulldown before searching as you suggested. The code below belongs in the PersonRelationManager Class, and helps when you are in the Family Edit and have the related People rows below and go to do an Attach.
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make()
->preloadRecordSelect(TRUE) // Loads the Person data in the pulldown.
->recordSelectSearchColumns(['full_name']) // Used when you Search when Attaching pulldown.
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make()
->preloadRecordSelect(TRUE) // Loads the Person data in the pulldown.
->recordSelectSearchColumns(['full_name']) // Used when you Search when Attaching pulldown.
])
I wanted to report back that I am ramping up on Filament and creating CRUD modules quickly with Attach/Detach features. Thank you both for your help!
Want results from more Discord servers?
Add your server