问题
I store data into array[], when i do dd(($request)); i can see the array with the date.
Question how i can store it inside the DB?
blade
<div class="form-group">
<label for="qualifiche">qualifiche:</label>
<input type="checkbox" name="array[]" value="uno" > Uno <br>
<input type="checkbox" name="array[]" value="due"> Due <br>
<input type="checkbox" name="array[]" value="tre"> Tre <br>
</div>
回答1:
You have two main options:
Option 1:
first Serialize the array and save it:
$arr = serialize($request['array']);
Then when you retrieve the data you do something like: // $model->attribute will be replaced with ur Model name and attribute $data = unserialize($model->attribute);
Here is the documentation: http://us.php.net/manual/en/function.serialize.php
Option 2:
Construct a string manually from the array and save it as a string:
// !!
is the delimiter you can use whatever u want...
$data = implode('!!', $request['array]);
and you can have the array back using:
$array = explode('!!', $model->attribute);
For Laravel:
You can write a getter to get the data as an array without doing it manually:
public function getSomethingAttribute($value)
{
// if using serialize
return unserialize($value);
// or if using implode
return explode('!!', $value)
}
Extra:
You can also store it as JSON if the database field is set as JSON or Text...if it's the case parse the array and save it.
来源:https://stackoverflow.com/questions/49259117/laravel-how-to-store-an-array-in-to-database