0

this is a part of my code:

<div class="row">
  <div class="col-lg-2 col-md-12 col-xs-12">
    <div class="form-group">
      <label for="exampleInputEmail1">Title*</label>
      <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="title[]">
    </div>
  </div>
  <div class="col-lg-5 col-md-12 col-xs-12">
    <div class="form-group">
      <label for="exampleInputEmail1">First Name*</label>
      <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="f_name[]">
    </div>
  </div>
  <div class="col-lg-5 col-md-12 col-xs-12">
    <div class="form-group">
      <label for="exampleInputEmail1">Last Name*</label>
      <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="l_name[]">
    </div>
  </div>
</div>

and if i click add button, it will show the second form same like above. so how to save all input data into database? this is what i've done in my controller.

public function store(Request $request){
    $title = $request->title;
    $f_name = $request->f_name;
    $l_name = $request->l_name;

    $form = new Conference;
    $form->name = $title.' '.$f_name.' '.$l_name;
    $form->email = $request->email;
    $form->phone = $request->phone;
    $form->position = $request->position;
    $form->category = $request->optradio;
    $form->price = $request->price;
    $form->t_price = $request->t_price;
    $form->company = $request->company;
    $form->save();
    return redirect()->back()->with('message','REGISTRATION SUCCESS');
  }

but it returned an error "Array to string conversion". I know i have to write something in my controller but i don't know what to do. can someone help me?

3
  • you're getting title, f_name, l_name in array. remove [] array. now your name will looks like title Commented Mar 3, 2020 at 8:42
  • is it ok to remove it? because i want to save multiple form with the same field at once Commented Mar 3, 2020 at 8:51
  • add foreach loop before your code. Commented Mar 3, 2020 at 9:05

1 Answer 1

1

You can loop through elements and then bulk insert as below

public function store(Request $request){

$title = $request->title;
$f_name = $request->f_name;
$l_name = $request->l_name;

$data = [];
foreach($title as $key => $value) {
    $data[] = [
        "name" => $value." ".$f_name[$key]." ".$l_name[$key];
    ] 
}


  $Conference::insert($data);
  return redirect()->back()->with('message','REGISTRATION SUCCESS');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.