Retriving album_id from a relationships - Select::make('album:id)
Hi guys, I'm a noob and I'm stucked with this thing... I actually have a double problem with this. Hope someone can help, i paste some code here below:
25 Replies
So I have the filament User who can have many photos and albums and albums can have multiple photo as well. When I create a new album I associate it to an user (with a modal) using this:
Right now, for the "photo situation" I have to link the id of the album and the id of the user to a photo. For the user was similar as I did before so THAT'S FINE. I'm having some trouble with the album.
-FIRST OF ALL: I select the album to pick up (to associate to a photo) whit this:
-SECOND OF ALL: I got stucked. Cause right now, as you can see, I can create really easily an album BUUUUUUT I absolutely can't associate none 'user_id' (cause If you open the modal to create a new album it ask for 'user_id' as well) neither I'm able to associate an 'album_id' to the new album I'm creating... I've tried to find something on documentation but nothing is working and I'm a real noob...
PLS HELP 😭
Sorry, I don't get the issue.
UUUUUUT I absolutely can't associate none 'user_id' (cause If you open the modal to create a new album it ask for 'user_id' as well)Can you show this? Where does it ask for the ID? Or is it an error from the DB? You didn't specify a
user_id
inside createOptionForm()
and it's a different Action than your first one. You can adjust the Action:
https://filamentphp.com/docs/3.x/forms/fields/select#customizing-the-select-action-objects
neither I'm able to associate an 'album_id' to the new album I'm creating... I've tried to find something on documentation but nothing is working and I'm a real noob...Can you explain this more? Also a DB issue? Or something else?
Hi ty for answering. It's not a db issue, I think it's just because i'm a noob. So as I said, when i create a new album it has to be associated to an user, right? That's why I have done this in the AlbumResource/Pages/ManageAlbums
The problem is when I try to do the same thing for the Photos upload... If I just had to link a photo to an user is easy, because I have in PhotoResource/Pages/CreatePhoto this code here:
BUT in the PhotoResource I have a Select that give me the possiblity to associate the photo to an album (TOTALLY FIND) or to create a new album in the same page of the photo uploading (BIG PROBLEM)
because here, during THIS SPECIFIC MODAL I really dunno how to link the user_id to the new album I'm creating, because this "modal-in-modal" don't use this code here:
So i'm not getting the user who create the album
FURTHERMORE if I'm using this
to retrieve the album title through its id, AT THE END, i'm not getting the id saved but received a NULL property on the database
BTW i dunno how to add this option to save in this specific case the album_id and the user_id with this code here
I think it's pretty easy but I'm noob af
Hope this is more clear :/
Try a combination of
->createOptionAction()
and ->mutateFormDataUsing()
Try removing the ->relationship()
or make the field nullable, because that saves after the record was created. You can use ->options(fn () => Album::pluck('title', 'id))
instead
What this mean? T_T
Might be possible, that this is only available in combination with
->relationship()
🤔Btw, using this with an existing album doesn't give me the album_id in the database... I'm just gettin the classi user_id
how do I solve all this? T_T
See my answer above. You probably need to make the database column nullable
Which one?
The "make the field nullable" answer.
No dude... ofc... I mean which column should be nullable? Cause if u mean the 'album_id' and 'user_id' they are already nullable
Ah right, you already wrote that you get
null
on DB, sorry 🙈
Hmm, weird one. Can you share the Photo
model with the album
relationship?Sure:
Photo Model:
Photo Migration:
Album Model:
Album Migration:
This is in the AlbumResource form, right?
These are the Models from Model. I Past the AlbumResource as well (but the problem is in the PhotoResource)
Sorry, I meant to PhotoResource. The code at the beginning was the
form()
method, right`?PhotoResource:
And this also doesn't save
album_id
for Photo? 🤔 Or is it just combined with ->relationship()
?yep it doesn't
nor If I create the album in the Photo page nor if I select an existing one.
What if you remove all methods from the Select apart from
->options()
Select::make('album_id')->options(...)
Yeah i just tried...
If I just have the album id to show up (with preload()) they don't show up
it's like i'm no getting the album ids at all...
it shows up the name but if I decide to see just ids they don't show up
Btw. the
album_id
issue might be because it is not fillable.
Can you share the code you used?I try to :fresh and I let u know
Jesus it worked -.- it was just the fillable problem.. I mean I've tried with an existing album and I'm getting the link both with user_id and album_id
now I try to create a new one
(from the PhotoResource)
I'm receiving this
because of this:
There is no
Album $record
in a CREATE option.Occhei... so... I don't knwo... I mean this last piece of code was just copy/pasted to see if something changed xD Is it correct? Do I have to keep it? And if yes how do I create this func in the create page? like literally.
(Sorry for the bothering dude but i'm noob af. Like a lot. I'm from fashion retail and I've just swapped my life)