I am getting input from checkbox values in array using bootstrap form. I am using array for storing checkbox values. How i convert this array to string . Because database only take string values.
Here is my code
<div class="form-group col-md-12">
    <div class="custom-control custom-checkbox custom-control-inline">
        <input type="checkbox" id="eduPrimary" name="education[]" 
        class="custom-control-input" value="primary" />
        <label class="custom-control-label" for="eduPrimary">primary</label>
    </div>
</div>
<div class="form-group col-md-12">
    <div class="custom-control custom-checkbox custom-control-inline">
        <input type="checkbox" id="eduSecondary" name="education[]" 
        class="custom-control-input" value="secondary" />
        <label class="custom-control-label" for="eduSecondary">secondary</label>
    </div>
</div>
<div class="form-group col-md-12">
    <div class="custom-control custom-checkbox custom-control-inline">
        <input type="checkbox" id="eduUniversity" name="education[]" 
        class="custom-control-input" value="university" />
        <label class="custom-control-label"for="eduUniversity">university</label>
    </div>
</div>
In backend i am using laravel to store values to database But it run error that storing array to string in mysql.
public function store(Request $request,AdProfile $adprofile)
{
    $adprofile->education = $request->education[];
    $adprofile->save();
    return redirect()->route('adprofile.profilecomplete');
}
                You can use php implode for this or you can also use laravel collection for this.
heres the exmaple
collect([1, 2, 3, 4, 5])->implode('-');
// '1-2-3-4-5'
see documentation for this Implode
or you can use php function implode
see this
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
//Hello World! Beautiful Day!
                         print_r($request->education); //It is an array print
$str_json = json_encode($request->education); //array to json string conversion
echo  $str_json; // printing json string
print_r(json_decode($str_json)); //printing array after convert json string to array
exit; // exiting further execution to check resutls
                        easy way is using json_encode and json_decode php functions.
if you want to store an array in string column you can use:
$adprofile->education = json_encode($array);
and if you want to get that from DB and convert it back to an array use:
$array = json_decode($adprofile->education);
In your "AdProfile" model add attribute casting variable so laravel will automatically convert array to json and json to array,
Like this
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AdProfile extends Model
{
    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'education' => 'array',
    ];
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With