在查询中保存批量数据 - laravel

时间:2018-04-03 17:53:11

标签: php laravel

我有一个输入字段,用于将数字输入数据库。现在,为输入的每个数字创建一个唯一的标记。

现在我的代码在下面,当我输入 phone token +144223202320,+4403224202340 dfsfsfsdfsdfdsfs 时,它会被保存到数据库中,但这两个数字都保存在同一列中,如

phone                              token  
+144223202320                1111112323242343
+4403224202340               dfsfsfsdfsdfdsfs 

但这就是我想要的

  public function send( Request $request)
      {
        do {
          //generate a random string using Laravel's str_random helper
          $token = str_random();
          }
         while (Invite::where('token', $token)->first());

         $invite = Invite::create([
            'phone' => $request->get('phone'),
            'token' => $token
        ]);

          return redirect()->back()->with('status','Message  successfully sent');

      }

控制器

<div>
  <ul>
    <li><a href="#turn-left">Turn left</a></li>
    <li><a href="#turn-right">Turn right</a></li>
  </ul>
</div>

<div class="possible-outcome">
  <span id="turn-left"></span>
  <p>You step into a puddle.</p>
</div>

<div class="possible-outcome">
  <span id="turn-right"></span>
  <p>You fall into a ditch.</p>
</div>

我怎么能这样做呢?

2 个答案:

答案 0 :(得分:0)

分发电话号码并在数据库中为每部手机写一条记录

public function send( Request $request)
{
     foreach(explode(",", $request->get('phone')) as $phone){
          do {
               //generate a random string using Laravel's str_random helper
               $token = str_random();
          }while (Invite::where('token', $token)->first());

          $invite = Invite::create([
               'phone' => $phone,
               'token' => $token
          ]);
     }

     return redirect()->back()->with('status','Message  successfully sent');

}

<强>更新

似乎在for循环中没有重新创建对象。改为使用这种方法

public function send( Request $request)
{
     foreach(explode(",", $request->get('phone')) as $phone){
          do {
               //generate a random string using Laravel's str_random helper
               $token = str_random();
          }while (Invite::where('token', $token)->first());

          $invite = new Invite;
          $invite->phone = $phone;
          $invite->token = $token;
          $invite->save();
     }

     return redirect()->back()->with('status','Message  successfully sent');

}

答案 1 :(得分:0)

您可以通过递归检查令牌来尝试此方法。 附:您可能需要进行一些调整才能100%完成代码工作。

public function send(Request $request){
    $phones = explode("," $request->phone);
    foreach($phones as $phone){
        $token = $this->generateUniqueInviteToken();
        if($token){
            $invite = new Invite();
            $invite->phone = $phone;
            $invite->token = $token;
            if($invite->save()){
                return redirect()->back()->with('status', 'Message  successfully sent');
            }
            return redirect()->back()->with('status', 'FAILED');
        }
    }
}

public function generateUniqueInviteToken(){
    $token = str_random();
    $checkedInvite = Invite::where("token", $token)->first();

    if(!$checkedInvite){
        return $token;
    }else{
        $this->generateUniqueInviteToken();
    }
}