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 Harrin14mo ago
probably missing options cast on the model, should be array
Absolium
Absolium14mo ago
No, this is in my model
Absolium
Absolium14mo ago
Absolium
Absolium14mo ago
I tried adding the options to fillable and cast
Dan Harrin
Dan Harrin14mo ago
should be 'options' => 'array'
Absolium
Absolium14mo ago
Absolium
Absolium14mo ago
This is what i have now, but I still have the same error
Dan Harrin
Dan Harrin14mo ago
should be $casts, not $cast
Absolium
Absolium14mo ago
That did not fix the problem either
Dan Harrin
Dan Harrin14mo 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
Absolium14mo ago
Because in my form I display all of the different options that are in the table options
Dan Harrin
Dan Harrin14mo ago
i see
Filament
Filament14mo 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
Absolium14mo ago
Absolium
Absolium14mo ago
Flare
Array to string conversion - The error occurred at http://localhost:8000/admin/cars/create
Absolium
Absolium14mo ago
Does this work?
Dan Harrin
Dan Harrin14mo ago
do you have an options column on your table
Absolium
Absolium14mo 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 Harrin14mo ago
please send your options relationship definition
Absolium
Absolium14mo ago
Here is my car table
Absolium
Absolium14mo ago
Absolium
Absolium14mo ago
Here are my car relationshps:
Absolium
Absolium14mo ago
this is my caroptions pivot table model
Absolium
Absolium14mo ago
And my options model
Absolium
Absolium14mo ago
Absolium
Absolium14mo ago
This is my options schema
Absolium
Absolium14mo ago
And my caroptions pivot table schema
Absolium
Absolium14mo ago
Sorry I just got off work
Dan Harrin
Dan Harrin14mo 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
Absolium14mo 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
Absolium14mo ago
Dan Harrin
Dan Harrin14mo ago
i can see, its still not right
Absolium
Absolium14mo ago
Dan Harrin
Dan Harrin14mo ago
definitely not right either
Absolium
Absolium14mo ago
fuck
Absolium
Absolium14mo ago
Absolium
Absolium14mo 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 Harrin14mo 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
Absolium14mo 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 Harrin14mo ago
uh no return $this->belongsToMany(Option::class, 'car_option')->using(CarOption::class); using() sets a custom pivot model which you have defined
Absolium
Absolium14mo ago
Oh okay thank you for explaining
Absolium
Absolium14mo 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
Absolium14mo ago
Absolium
Absolium14mo ago
Absolium
Absolium14mo ago
Flare
Array to string conversion - The error occurred at http://localhost:8000/admin/cars/create
Dan Harrin
Dan Harrin14mo ago
can you send the entire CarResource and CreateCar page code copy it rather than screenshot
Absolium
Absolium14mo 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 Harrin14mo ago
wait... why are you overriding save()
Absolium
Absolium14mo ago
I read somewhere that i had to add the getState to save the relationship
Dan Harrin
Dan Harrin14mo 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
Absolium14mo 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
Absolium14mo ago
Absolium
Absolium14mo 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 Harrin14mo ago
the dd() should be on the window its not in the console so you have a CarObserver or something
toeknee
toeknee14mo ago
It will also be in the network logs
Absolium
Absolium14mo ago
It's not shown there Or there
Dan Harrin
Dan Harrin14mo ago
show me the mutateFormDataBeforeSave() with the dd()
Absolium
Absolium14mo 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 Harrin14mo ago
also isnt it supposed to be mutateFormDataBeforeCreate() on this page?
Absolium
Absolium14mo ago
Dan Harrin
Dan Harrin14mo ago
create for create pages save for edit pages
Absolium
Absolium14mo 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 Harrin14mo 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
Absolium14mo ago
No I don’t, do i need to make one? Is that another function in car model?
Dan Harrin
Dan Harrin14mo ago
no im just checking createcar page
Absolium
Absolium14mo ago
Absolium
Absolium14mo ago
This is a dd of the handlerecordcreation function
Dan Harrin
Dan Harrin14mo ago
wait... what are pictures @absolium you forgot $casts = ['pictures' => 'array'] thats it
Absolium
Absolium14mo 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
IndomieRendang14mo ago
You have to give him ⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐ rating for his totality 😆
_.shapeshifter
_.shapeshifter14mo ago
a rating of 100/10 😄
Absolium
Absolium14mo ago
Deffo, same for his patience