对不起,我是 Laravel 的新手,第一次尝试保存到数据库。我试图将数组保存到数据库,但出现错误“数组到字符串转换”。我尝试将迁移文件中的字符串值更改为其他选项,但出现了同样的错误。
Controller
public function store(Request $request)
{
Myroutes::create([ //posting to acc table
'start' => $request->start,
'end' => $request->end,
'waypoints' => $request->waypoints
]);
return redirect('/');
}
迁移
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();
});
}
模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Myroutes extends Model
{
protected $fillable = [
'user_id',
'start',
'end',
'waypoints'
];
}
查看
<div id="dynamicInput" class="form-group">
<label>Additional Destinations</label>
<input type="text" name="waypoints[]" class="form-control" autocomplete="on">
</div>
数据库表
最佳答案
protected $casts = [
'theme_setting' => 'array'
];
只需在类中的模型中使用转换
关于arrays - "Array to string conversion"错误 Laravel - 尝试将数组保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49985727/