php 转数字sql,php – 数组到字符串的转换(SQL:insert into

我正在尝试从Laravel 5.6中的Seeder插入数据,而我遇到了json类型的字段问题.我希望这个字段(‘stops’)是一个数组(例如10个整数不重复).

表播种器(RoutesTableSeeder.php)是这样的:

use \Illuminate\Support\Facades\DB;

use Illuminate\Database\Seeder;

use Faker\Factory as Faker;

use App\Models\Route;

class RoutesTableSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

//factory(Route::class, 20)->create();

$faker = Faker::create();

//$values= array();

/*for($i=0; $i < 10; $i++) {

$values []= $faker->unique()->randomDigit;

}

print_r(json_encode($values));*/

foreach (range(1, 20) as $index)

{

$values = array();

for($i=0; $i < 10; $i++) {

$values []= $faker->unique()->randomDigit;

}

//print_r($values);

DB::table('routes')->insert([

'user_id' => $faker->numberBetween($min = 1, $max = 20),

'name' => $faker->name,

'description' => $faker->name,

'route_photo' => $faker->image($dir = null, $width = 640, $height = 480, $category = null, $fullPath = true, $randomize = true, $word = null),

'stops'=> [

//$values,

json_encode($values)

//implode(", ", $values)

],

]);

}

}

}

我尝试了几种插入数据的方法.当我使用json_encode($values)时,我有以下错误:

Array to string conversion

(SQL: insert into `routes` (`user_id`, `name`, `description`, `route_photo`, `stops`)

values (19, Isaac

Feil, Holly Nolan, /tmp/bc8a3cf5e015d3afa96317485499e0ca.jpg,

[8,6,0,7,3,1,5,2,4,9]))

例如,这种价值[8,6,0,7,3,1,5,2,4,9]是我想要存储在’停止’字段中的,但我不知道出了什么问题….

拜托,你能帮我这么好吗?我很绝望….

如果它有帮助我发布模型:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Route extends Model

{

protected $fillable = [

'user_id',

'name',

'description',

'route_photo',

'stops'

];

protected $casts = [

'stops' => 'array'

];

}

迁移:

public function up()

{

Schema::create('routes', function (Blueprint $table) {

$table->increments('id');

//FK:users

$table->integer('user_id')->unsigned();

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');

//FK:users

$table->string('name');

$table->string('description')->nullable();

$table->string('route_photo');

$table->json('stops');

$table->timestamps();

});

}

非常感谢!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值