Many to Many relationship error: Array to String conversion
Hi guys, I'm trying to use filamentphp to keep an overview of cars.
I have a table Cars and a table Options (which comes with predefined options).
An option can belong to many Cars.
I seeded my database and i can view the cars with their respective options, I can edit cars and their options, but when I create a car I run into the following error: Array to String conversion.
You can see how I made my form in this picture.
Any help would be appreciated. And if there is any extra info you would need to asses the problem I'll send it
73 Replies
probably missing options cast on the model, should be
array
No, this is in my model
I tried adding the options to fillable and cast
should be
'options' => 'array'
This is what i have now, but I still have the same error
should be
$casts
, not $cast
That did not fix the problem either
wait
you're using relationship()?
why is that
because in which case, you dont need the cast, or a column on your model at all
Because in my form I display all of the different options that are in the table options
i see
We need more information to help you debug your problem. Please click on the top left 'SHARE' button of the error page you're seeing and share the link with us.
Does this work?
do you have an
options
column on your tableNo I made a pivot table called caroptions which contains the id's of the cars with id's of the options
please send your options relationship definition
Here is my car table
Here are my car relationshps:
this is my caroptions pivot table model
And my options model
This is my options schema
And my caroptions pivot table schema
Sorry I just got off work
shouldnt the Options model be Option instead
and Options hasMany CarOptionHelperModel, not Options
and you need to define the pivot table for the cars() relationship as car_options
please revise the Eloquent BelongsToMany documentation because there are quite a few things not following convention
Oh damn okay I will check out the BelongToMany documentation again
Sorry I'm coming more from a security background starting web development
Okay so I have read the documentation and made some changes to my code, but I keep receiving the same error. I'll send the updated code
i can see, its still not right
definitely not right either
fuck
And I changed the name of my table to car_option
Could you point out what I'm doing wrong please?
Car
- brand belongsto Brand
- engine belongsto Engine
- options belongstomany Option, 'car_options'
using(CarOption::class)
Option
- cars belongstomany Car, 'car_options' using(CarOption::class)
Car_Option should extend Pivot instead of Model, and it should be CarOption instead of Car_Option. And it should have $table = 'car_option'
and you do not need any casts on any of the modelsCould you please explain what you mean by this? Do you mean it like this?
public function options(): BelongsToMany
{
return $this->belongsToMany(Option::class, CarOption::class);
}
uh no
return $this->belongsToMany(Option::class, 'car_option')->using(CarOption::class);
using() sets a custom pivot model which you have definedOh okay thank you for explaining
Okay so I've made the changes that you have suggested, But i'm still experiencing the same issue. Here is my updated code:
can you send the entire CarResource and CreateCar page code
copy it rather than screenshot
the mutateFormDataBeforeSave is a function i added as i was trying to debug things
wait...
why are you overriding save()
I read somewhere that i had to add the getState to save the relationship
noo
we do that for you
you can delete the entire save()
and then add in a
dd($data)
in mutateFormDataBeforeSave() so i can see what is attempting to be savedOkay, Thank you for clarifying this
Give me a minute I'm struggling to find the output of the dd
As I still receive the error of array to string conversion but can't seem to see the output of the dd
I do get this where it tries to get car_resource instead of CarResources i think
And my dd doesn't seem to get triggered
the dd() should be on the window
its not in the console
so you have a CarObserver or something
It will also be in the network logs
It's not shown there
Or there
show me the mutateFormDataBeforeSave() with the dd()
I've tried to add a Log::debug($data) which also doesn't get displayed when i do tail laravel.logs
also isnt it supposed to be mutateFormDataBeforeCreate() on this page?
create for create pages
save for edit pages
You are correct
This is my dd
Somehow my options don't seem to be in here
in handlerecordcreation, what is the $data there
do you have a carobserver?
thats a good thing, we dont want them to be saved at this point, but i dont understand why you have the error
No I don’t, do i need to make one?
Is that another function in car model?
no im just checking
createcar page
This is a dd of the handlerecordcreation function
wait...
what are
pictures
@absolium you forgot $casts = ['pictures' => 'array']
thats itOmfg
You are right
I’ve been searching for this for so long
Thank you so much for all of the help and patience @danharrin
You have to give him ⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐ rating for his totality 😆
a rating of 100/10 😄
Deffo, same for his patience