"Array to string conversion" errorr Laravel - Trying to save array to database
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
Related videos on Youtube
highfly
Updated on June 04, 2022Comments
-
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
-
Dave Carruthers about 6 yearsIs waypoints supposed to be an array? any your storing it as a string?.
-
highfly about 6 yearsI 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 about 6 yearsThank you, that worked! Will I not be able to retrieve the waypoints and have each inserted into an input field?
-
Tim Lewis about 6 yearsYou 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 ofWaypoint
models. Have a read: laravel.com/docs/5.6/eloquent-relationships; useful stuff to know regardless. -
Indra about 6 yearsor save that as a json field in the db and cast it to array
-
Tim Lewis about 6 years@Indra Absolutely, a
JSON
column would work too, so long as the RDBMS supports it. -
highfly about 6 yearsI 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 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 over 4 years"implode" and "explode" need a delimiter, why not using "serialize" and "unserialize" for delegating the formatting to PHP?
-
Tim Lewis over 4 years@PascalWirtz Yes,
serialize()
andunserialize()
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.