是否可以在不使用 laravel 5.4 循环的情况下为特定用户存储多个 phone 号码

is it possible to store the multiple phone number for a specific user without using loop in laravel 5.4

我在 laravel 5.4,

有一段恋情

在用户模型中

public function phones()
    {
        return $this->hasMany(UserPhone::class);
    }

在 UserPhone 模型中

public function user()
   {
        return $this->belongsTo(User::class);
   }

现在,如果我要从一个表格中输入多个电话记录,例如:

<input type="text" name="phone[]">
<input type="text" name="phone[]">
<input type="text" name="phone[]">

等等, 我如何为当前登录到 laravl5.4 的特定用户存储这些电话???

试试这个

$user = User::find($id) ;
$user->phones()->saveMany([
new UserPhone(['phone'=> Input::get('phone')[0] ]) ,
new UserPhone(['phone'=> Input::get('phone')[1] ]),
...
]);
$contact = [];
   foreach ($current_user_contact->phone as $key => $value) {
                array_push($contact, new UserPhone(['phone' => $value]));
   }
$current_user->phones()->saveMany($contact);

没有for循环我找不到解决它的方法,不得不用上面提到的方法..和Milad

提到的一样
$newUser = User::findOrFail(1);
    $phones = [
        ['phone' => '111111111'],
        ['phone' => '111111112'],
        ['phone' => '111111113'],
        ['phone' => '111111114'],
    ];

    foreach ($phones as $phone) {
        $phone = new UserPhone($phone);
        $phone->user()->associate($newUser);
        $phone->save();
    }