Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert array to string in laravel?

Tags:

php

mysql

laravel

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');
}
like image 385
Assad Yaqoob Avatar asked Apr 24 '19 08:04

Assad Yaqoob


4 Answers

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!
like image 149
Karan Sadana Avatar answered Oct 20 '22 09:10

Karan Sadana


 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
like image 32
flik Avatar answered Oct 20 '22 07:10

flik


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

like image 6
Ali_Hr Avatar answered Oct 20 '22 08:10

Ali_Hr


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',
    ];
}
like image 3
Mihir ajagia Avatar answered Oct 20 '22 07:10

Mihir ajagia