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
Dan Harrin
Dan Harrin2y ago
probably missing options cast on the model, should be array
Absolium
AbsoliumOP2y ago
No, this is in my model
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
I tried adding the options to fillable and cast
Dan Harrin
Dan Harrin2y ago
should be 'options' => 'array'
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
This is what i have now, but I still have the same error
Dan Harrin
Dan Harrin2y ago
should be $casts, not $cast
Absolium
AbsoliumOP2y ago
That did not fix the problem either
Dan Harrin
Dan Harrin2y ago
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
Absolium
AbsoliumOP2y ago
Because in my form I display all of the different options that are in the table options
Dan Harrin
Dan Harrin2y ago
i see
Filament
Filament2y ago
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.
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
Flare
Array to string conversion - The error occurred at http://localhost:8000/admin/cars/create
Absolium
AbsoliumOP2y ago
Does this work?
Dan Harrin
Dan Harrin2y ago
do you have an options column on your table
Absolium
AbsoliumOP2y ago
No I made a pivot table called caroptions which contains the id's of the cars with id's of the options
Dan Harrin
Dan Harrin2y ago
please send your options relationship definition
Absolium
AbsoliumOP2y ago
Here is my car table
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
Here are my car relationshps:
Absolium
AbsoliumOP2y ago
this is my caroptions pivot table model
Absolium
AbsoliumOP2y ago
And my options model
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
This is my options schema
Absolium
AbsoliumOP2y ago
And my caroptions pivot table schema
Absolium
AbsoliumOP2y ago
Sorry I just got off work
Dan Harrin
Dan Harrin2y ago
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
Absolium
AbsoliumOP2y ago
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
Absolium
AbsoliumOP2y ago
Dan Harrin
Dan Harrin2y ago
i can see, its still not right
Absolium
AbsoliumOP2y ago
Dan Harrin
Dan Harrin2y ago
definitely not right either
Absolium
AbsoliumOP2y ago
fuck
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
And I changed the name of my table to car_option Could you point out what I'm doing wrong please?
Dan Harrin
Dan Harrin2y ago
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 models
Absolium
AbsoliumOP2y ago
Could 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); }
Dan Harrin
Dan Harrin2y ago
uh no return $this->belongsToMany(Option::class, 'car_option')->using(CarOption::class); using() sets a custom pivot model which you have defined
Absolium
AbsoliumOP2y ago
Oh okay thank you for explaining
Absolium
AbsoliumOP2y ago
Okay so I've made the changes that you have suggested, But i'm still experiencing the same issue. Here is my updated code:
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
Flare
Array to string conversion - The error occurred at http://localhost:8000/admin/cars/create
Dan Harrin
Dan Harrin2y ago
can you send the entire CarResource and CreateCar page code copy it rather than screenshot
Absolium
AbsoliumOP2y ago
<?php

namespace App\Filament\Resources\CarResource\Pages;

use App\Filament\Resources\CarResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\CreateRecord;
use Psy\Readline\Hoa\Console;

class CreateCar extends CreateRecord
{
protected static string $resource = CarResource::class;

protected function getRedirectUrl(): string
{
return $this->previousUrl ?? $this->getResource()::getUrl('index');
}

protected function mutateFormDataBeforeSave(array $data): array
{
// $data['last_edited_by_id'] = auth()->id();
var_dump($data);
return $data;
}

public function save(): void
{
$this->car->update(
$this->form->getState()
);
}
}
<?php

namespace App\Filament\Resources\CarResource\Pages;

use App\Filament\Resources\CarResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\CreateRecord;
use Psy\Readline\Hoa\Console;

class CreateCar extends CreateRecord
{
protected static string $resource = CarResource::class;

protected function getRedirectUrl(): string
{
return $this->previousUrl ?? $this->getResource()::getUrl('index');
}

protected function mutateFormDataBeforeSave(array $data): array
{
// $data['last_edited_by_id'] = auth()->id();
var_dump($data);
return $data;
}

public function save(): void
{
$this->car->update(
$this->form->getState()
);
}
}
the mutateFormDataBeforeSave is a function i added as i was trying to debug things
Dan Harrin
Dan Harrin2y ago
wait... why are you overriding save()
Absolium
AbsoliumOP2y ago
I read somewhere that i had to add the getState to save the relationship
Dan Harrin
Dan Harrin2y ago
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 saved
Absolium
AbsoliumOP2y ago
Okay, 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
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
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
Dan Harrin
Dan Harrin2y ago
the dd() should be on the window its not in the console so you have a CarObserver or something
toeknee
toeknee2y ago
It will also be in the network logs
Absolium
AbsoliumOP2y ago
It's not shown there Or there
Dan Harrin
Dan Harrin2y ago
show me the mutateFormDataBeforeSave() with the dd()
Absolium
AbsoliumOP2y ago
I've tried to add a Log::debug($data) which also doesn't get displayed when i do tail laravel.logs
Dan Harrin
Dan Harrin2y ago
also isnt it supposed to be mutateFormDataBeforeCreate() on this page?
Absolium
AbsoliumOP2y ago
Dan Harrin
Dan Harrin2y ago
create for create pages save for edit pages
Absolium
AbsoliumOP2y ago
You are correct
array:19 [▼ // app/Filament/Resources/CarResource/Pages/CreateCar.php:26
"brand_id" => "6"
"model" => "A10"
"engine_id" => "3"
"horse_power" => "2"
"transmission" => "manual"
"cylinder_capacity" => "2"
"doors" => "2"
"seats" => "2"
"car_color" => "Gray"
"interior_color" => "Gray"
"upholstery" => null
"description" => null
"thumbnail" => null
"pictures" => []
"kilometer_reading" => 0
"num_previous_owners" => "4"
"first_use" => null
"service_history" => null
"accident_history" => null
]
array:19 [▼ // app/Filament/Resources/CarResource/Pages/CreateCar.php:26
"brand_id" => "6"
"model" => "A10"
"engine_id" => "3"
"horse_power" => "2"
"transmission" => "manual"
"cylinder_capacity" => "2"
"doors" => "2"
"seats" => "2"
"car_color" => "Gray"
"interior_color" => "Gray"
"upholstery" => null
"description" => null
"thumbnail" => null
"pictures" => []
"kilometer_reading" => 0
"num_previous_owners" => "4"
"first_use" => null
"service_history" => null
"accident_history" => null
]
This is my dd Somehow my options don't seem to be in here
Dan Harrin
Dan Harrin2y ago
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
Absolium
AbsoliumOP2y ago
No I don’t, do i need to make one? Is that another function in car model?
Dan Harrin
Dan Harrin2y ago
no im just checking createcar page
Absolium
AbsoliumOP2y ago
Absolium
AbsoliumOP2y ago
This is a dd of the handlerecordcreation function
Dan Harrin
Dan Harrin2y ago
wait... what are pictures @absolium you forgot $casts = ['pictures' => 'array'] thats it
Absolium
AbsoliumOP2y ago
Omfg You are right I’ve been searching for this for so long Thank you so much for all of the help and patience @danharrin
IndomieRendang
You have to give him ⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐ rating for his totality 😆
_.shapeshifter
a rating of 100/10 😄
Absolium
AbsoliumOP2y ago
Deffo, same for his patience
Want results from more Discord servers?
Add your server