0

我想在我的mysql数据库中添加数组,为此我创建一个控制器,在该控制器中我接受来自邮递员的数组并通过将'[]'添加到phone_no来从邮递员发送数据。我的控制器:

$rules=[
            'name' => 'required',
            'password'  => 'required',
            'phone_no'=>'required', 
 ];

        $validator=Validator::make($request->all(),$rules,[
            'phone_no.*'=>'required',
        ]
    );
        if ($validator->fails()) {
            return response()->json($validator->errors(),400);
        }
        $user=Model::create($request->all());

我的模型:

protected $fillable =[
            'name',
            'password',
            'phone_no',
 ];

但它在我的数据库中没有添加任何内容,添加了其他属性但我的 phone_no 列是空的

4

1 回答 1

0

您不能在 mysql 数据库中存储数组。您可以将其序列化或将其转换为 json。我的建议是像这样使用“Array and JSON Casting”:

https://laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting

将此添加到您的模型中:

protected $casts = [
    'phone_no' => 'array',
];
于 2020-10-28T20:07:32.590 回答