Reputation: 55
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();
}
Upvotes: 3
Views: 2782
Reputation: 4674
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.
Upvotes: 5