0

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.

3
  • 1
    Store implode(" ", $data); into some variable and use that variable in update function. Commented Jun 13, 2022 at 14:12
  • how could I add it to the update function? (the function is: $user->where('id',$data['id'])->update($data);) Commented Jun 13, 2022 at 14:14
  • <option value="{{$product->name}}">{{$product->name}}</option> creates unnecessary markup bloat. You never need to declare the value attribute if it is identical to the option's text. Commented Jun 13, 2022 at 23:55

1 Answer 1

0

You can use laravel casts which will convert the array to JSON (string) and store into the database, and when you will take data from DB it will be converted to an array again, so you can use it to display selected items in multiselect

class Combos extends Model
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'description' => 'array',
    ];
}

This feature is available for laravel 5.5 too

Sign up to request clarification or add additional context in comments.

2 Comments

Is your advice already provided here? Is it already provided somewhere else (better or earlier)? It is important to not add redundant content to Stack Overflow. If a question can be resolved by pointing someone to an appropriate pre-existing page on SO, please do that instead of answering.
@mickmackusa Hi. Yes, this could be the answer to this. But this case is easier. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.