"Array to string conversion" errorr Laravel - Trying to save array to database

12,123

Solution 1

You've got an error: 'waypoints' => $request->waypoints won't work, as $request->waypoints is an array, and you can't save an array into a VARCHAR() field. If you implode the input, and convert it to a comma-separated string it should work alright:

`'waypoints' => implode(",", $request->waypoints`)

That being said, this is generally considered a bad idea; consider using relationships between Routes and Waypoints as separate tables, for both clarity and ease of use (especially in retrieving/editing.)

Solution 2

I used this for laravel 5.8

$array = $request->names;
$array = implode(',', $array);
$request['names'] = $array;
$distribute = User::create($request->all());

Solution 3

protected $casts = [
        'theme_setting' => 'array'
];

Just use casting in Model within Class

Share:
12,123

Related videos on Youtube

highfly
Author by

highfly

Updated on June 04, 2022

Comments

  • highfly
    highfly almost 2 years

    Sorry Im new to Laravel and trying to save to the database for the first time. Im trying to save an array to the database but the error "array to string conversion" is appearing. I've tried changing the string value in the migration file to other options but the same error is appearing.

    Controller

        public function store(Request $request)
    {   
        Myroutes::create([ //posting to acc table
            'start' => $request->start,
            'end' => $request->end,
            'waypoints' => $request->waypoints
        ]);
    
        return redirect('/');
    }
    

    migration

     public function up()
    {
        Schema::create('myroutes', function (Blueprint $table) {
            $table->increments('myroute_id');
            $table->integer('user_id');
            $table->string('start');
            $table->string('end');
            $table->string('waypoints');
            $table->timestamps();
        });
    }
    

    model

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Myroutes extends Model
    {
        protected $fillable = [
            'user_id',
            'start',
            'end',
            'waypoints'
        ];
    }
    

    View

    <div id="dynamicInput" class="form-group">
                        <label>Additional Destinations</label>
                        <input type="text" name="waypoints[]" class="form-control" autocomplete="on">
                    </div>
    

    Database table

    database-img

    • Dave Carruthers
      Dave Carruthers about 6 years
      Is waypoints supposed to be an array? any your storing it as a string?.
    • highfly
      highfly about 6 years
      I thought I need "waypoints[ ]" to make the google maps waypoints work, I've now change the name to just "waypoints", but only the first waypoint is submitting to the database. (All above code is still the same)
  • highfly
    highfly about 6 years
    Thank you, that worked! Will I not be able to retrieve the waypoints and have each inserted into an input field?
  • Tim Lewis
    Tim Lewis about 6 years
    You can forsure, but it's extra logic to pull a string, separate by commas, etc etc, as opposed to being able to call Route->waypoints and retrieve a nice array of Waypoint models. Have a read: laravel.com/docs/5.6/eloquent-relationships; useful stuff to know regardless.
  • Indra
    Indra about 6 years
    or save that as a json field in the db and cast it to array
  • Tim Lewis
    Tim Lewis about 6 years
    @Indra Absolutely, a JSON column would work too, so long as the RDBMS supports it.
  • highfly
    highfly about 6 years
    I was was worried the google maps waypoints would not work without the squared brackets in the name. I removed it and it still works, but now only the first waypoint submits to the database as I changed it back to 'waypoints' => $request->waypoints.
  • highfly
    highfly about 6 years
    @TimLewis check out my new question on trying to get it to post from database - thanks! stackoverflow.com/questions/50045184/…
  • Rachel Wirtz
    Rachel Wirtz over 4 years
    "implode" and "explode" need a delimiter, why not using "serialize" and "unserialize" for delegating the formatting to PHP?
  • Tim Lewis
    Tim Lewis over 4 years
    @PascalWirtz Yes, serialize() and unserialize() would also work (if you store the serialized value and unserialize it on retrieval), but also has the same issue that it's not the best idea. You should be able to query data, and storing the result of a PHP function generally removes the ability to do that.