22

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');
}
6
  • 1
    php.net/manual/en/function.implode.php Commented Apr 24, 2019 at 8:13
  • can we see your adProfile model? Commented Apr 24, 2019 at 8:14
  • What does this $request->education[] do? Commented Apr 24, 2019 at 8:14
  • Across any programming language, it is impossible to implicitly convert an array to a string. Use the implode() function to iterate through the instances of the array and assign it to a string as @Tarasovych said. Commented Apr 24, 2019 at 8:15
  • 1
    implode(" ",$request->education) Commented Apr 24, 2019 at 8:15

8 Answers 8

20

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!
Sign up to request clarification or add additional context in comments.

Comments

9
 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

1 Comment

Its working. Thanks @flik for making this easy for me.
8

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);

Comments

3

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',
    ];
}

Comments

2

You can use implode function.

Manual: https://www.php.net/manual/en/function.implode.php

Now, your code will like:

public function store(Request $request,AdProfile $adprofile)
{
    $adprofile->education = implode(',', $request->education[]);
    $adprofile->save();
    return redirect()->route('adprofile.profilecomplete');
}

1 Comment

I do exact same of your code it gives me FatalThrowableError Cannot use [] for reading what is the problem
1

try this.

$variable = print_r($someArray,1)

Comments

0

Laravel comes with the Illuminate\Support\Arr facade, full of methods for array manipulation.

One of them is join.

/**
 * Join all items using a string. The final items can use a separate glue string.
 *
 * @param  array  $array
 * @param  string  $glue
 * @param  string  $finalGlue
 * @return string
 */
public static function join($array, $glue, $finalGlue = '')

To me, the correct way to convert an array to string with Laravel would be:

\Arr::join($array, ',');

Comments

-3

you can do something like this :-

           <?php
            $arr=array("this","is","an","array");
            
            echo "array elements are"."<br>";
            
            foreach($arr as $value)
             echo $value."<br>";
            
            echo "The string is "."<br>";
            ?>
         @foreach($arr as $value)
         {!! $value !!}
         @endforeach

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.