简体   繁体   中英

Laravel get each of lines from textarea and insert into new row in database

I'm trying to get each of lines from textarea and insert into new row in database. So, when I write a list of text with new brakelines f.ex:
test1
test2
test3

I want to insert each of these multilines into new row in MySql f.ex:
id kwName
1. test1
2. test2
3. test3

PLease, help me.

My code is:

Blade

<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">

Controller

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();
}

I think this will help you::

<?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. 
  }
?>

This is just example use it as your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM