Hi.
I have 3 tables: files, product_types and screen_colors.
In the screen_colors table I have 2 types of colors. I want to be able to assign a product type and a screen color type to each file. I created a pivot table with 3 foreign keys: file_id, product_type_id and screen_color_id and a relationship in File Model.
public function product_types()
{
return $this->belongsToMany(ProductType::class)->withPivot('screen_color_id');
}
How can I implement a product type and screen color to the file?
This is the method I tried but I get error because screen_color can have different colors for different product types
(2/2) QueryException
Array to string conversion....
View:
@foreach($product_types as $product_type)
<tr>
<td class="checkbox-input">
<input type="checkbox" name="product_type[{{$product_type->id}}]" value="{{$product_type->id}}">
</td>
<td>{{$product_type->brand}}</td>
<td>{{$product_type->model}}</td>
<td> {{$product_type->variant}}</td>
<td>
@if(in_array($product_type->model, $screen_color_items))
@foreach($screen_colors as $screen_color)
<div>
<input type="checkbox" name="screen_color[{{$screen_color->id}}]" value="{{$screen_color->id}}">
<span>{{$screen_color->name}}</span>
</div>
@endforeach
@else
Standard
@endif
</td>
</tr>
@endforeach
Controller
$last_file_created = File::orderBy('id', 'desc')->first();
$last_file_created->product_types()->attach($request->input(['product_type']), ['screen_color_id' => $request->input(['screen_color'])]);
I am also attaching a reference picture of the table on the page for adding a file.
https://ibb.co/52rPw0c
I.E
file_id | product_type_id | screen_color_id
---------+-----------------+-----------------
1 | 2 | 1
1 | 2 | 2
1 | 3 | 1
1 | 4 | 2