Laravel从文本区域获取每一行并插入到数据库中的新行中



我正在尝试从文本区域中获取每一行并插入到数据库中的新行中。因此,当我编写带有新制动线f.ex的文本列表时:
测试1
测试2
测试3

我想将这些多行中的每一个插入到 MySql f.ex 中的新行中:
id kwName
1. 测试1
2. 测试2
3. 测试3

求你,帮帮我。

我的代码是:

叶片

<textarea rows="1" name="kwName" class="form-control" placeholder="Insert keywords list"></textarea>
<input type="submit" class="btn btn-primary" name="add_kw" value="save">

控制器

if ($request->has('add_kw')) {
                $this->validate($request,[
                  'kwName'=> 'required',
                ]);
                // create new data              
                $values = new keyword;
                $values->kwName = $request->kwName;
                $values->website_id = $id;
                $values->save();
}

我认为这会对你有所帮助:

<?php
  $text = trim($_POST['textareaname']); 
  $textAr = explode("n", $text);  // remove the last n or whitespace character
  $textAr = array_filter($textAr, 'trim'); // remove any extra r characters left behind
  foreach ($textAr as $line) {
      // processing here. 
  }
?>

这只是根据您的需要使用它的示例。

最新更新