I'm having an issue with an update function on laravel, I'm trying to send data from a multiselect in a form, I want the description of "combos" to have several items stored, and those items I grab them from another table (productos) and put them in a select inside the form. But the problem is that since I'm sending several items I need to send them as an array to store it into the database then I get the error
array to string conversion
Here's the function on my controller
public function update(Combos $product){
$data = request()->validate([
'name' => 'required|string|min:8|max:100',
'id' => 'required',
'description' =>'required',
]);
$user->where('id',$data['id'])->update($data);
return view('Combos')->with('combos', Combos::all())
->with('products', Products::all());
}
Since I'm sending the values through a select in the form:
<div class="form-group" id="selector">
<label for="">Products</label>
<select multiple="multiple" class="form-control" name="description[]" >
<option value="" disabled>Select product</option>
@foreach ($products as $product)
<option value="{{$product->name}}">{{$product->name}}</option>
@endforeach
</select>
</div>
Is there any way I can pass the list of data as an string (because the column on the database is a string). I've tried using implode() by putting $data['description] inside a variable like this:
public function update(Combos $product){
$data = request()->validate([
'name' => 'required|string|min:8|max:100',
'id' => 'required',
'description' =>'required',
]);
$new_desc = implode(" ", $data['description']);
dd($new_desc); //prints "value1 value2 value3" as string, instead of an array
$product->where('id',$data['id'])->update($data);
return view('Combos')->with('combos', Combos::all())
->with('products', Products::all());
}
the curious thing is that when I dd($new_desc) it does convert the array into a string
Or straight up just placing the data['description] inside the implode(), but even then it's not working.
public function update(Combos $product){
$data = request()->validate([
'name' => 'required|string|min:8|max:100',
'id' => 'required',
'description' =>'required',
]);
implode(" ", $data['description']);
dd($data['description']); //when I submit the update it still sends the data as an array array:2[0 => 'value1', 1 => value2]
$user->where('id',$data['id'])->update($data);
return view('Combos')->with('combos', Combos::all())
->with('products', Products::all());
}
What could I do in this case to send this list of items from the multiselect as a string instead of an array, or how do I convert them to string before sending the update? I would be really thankful if someone could help me, thanks in advance.
$user->where('id',$data['id'])->update($data);)<option value="{{$product->name}}">{{$product->name}}</option>creates unnecessary markup bloat. You never need to declare thevalueattribute if it is identical to the option's text.