1

I want to insert dynamic fields into DB. I'm using the following code but it does not work as I expect.

<html>
<input id="reporting" type="text" value="salman" name="reporting[]">    
<input id="reporting" type="text" value="ankur" name="reporting[]">    
</html>

<?php

 $report = Input::get('reporting');

 for($i=0; $i<=count($report);$i++)
        {
            $news = new Reporting();
            $news->user_id = 1;
            $news->reporting = $report;
            $news->save();
        }
?>

expected result:

user_id || reporting
1           Salman
1           Ankur  

Can you guys please help me to fix this.

4
  • What does dd($report) show? Commented Nov 26, 2017 at 13:52
  • 1
    $news->reporting = $report[$i]; Commented Nov 26, 2017 at 13:52
  • dd($report) shows 'salman' Commented Nov 26, 2017 at 13:58
  • i insert id number 25 and when i use this method $news->reporting = $report[$i]; it insert 2 rows in db but in first row inserts id number 2 and in second row insert id number 5 Commented Nov 26, 2017 at 13:59

2 Answers 2

1

As $report is an array, current item of it can be received with [] notation:

$report = Input::get('reporting');

for($i=0; $i<=count($report);$i++)
{
    $news = new Reporting();
    $news->user_id = 1;
    $news->reporting = $report[$i];    // here add [$i]
    $news->save();
}
Sign up to request clarification or add additional context in comments.

5 Comments

ya i tried this $news->reporting = $report[$i]; // here add [$i] and it inserts 2 rows in db but im using id 25 so it splits the id in both rows, in first it shows id# 2 and in second row id #5
How do you send your data to server? With ajax or not? If with ajax - show ajax function.
Very strange then. Assuming your form fields are correctly named reporting[], then Input::get must return array, not single field. Try print just $_POST['reporting'], if it is not array, then it's not laravel problem.
i restart the server and than tried again it inserts correct values but shows the error (1/1) ErrorException Undefined offset: 2
Remove = -> $i<count($report)
0

You could map the collection and create a new report while storing value of reports the way you want:

<html>
<body>
@if(session('success'))
<div class="alert alert-success">
    {{ session('success') }}
</div>
@endif
<form action="/" method="post">
  {{csrf_field()}}
  <input id="reporting" type="text" value="salman" name="reporting[]">    
  <input id="reporting" type="text" value="ankur" name="reporting[]">  
  <button type ="submit"> Send </button>
</form>  
</body>
</html>

Catch data on backend:

public function store()
{
    $fields = collect(Input::get('reporting'));

    $fields->map(function($value, $key){

        return Reporting::create([

            'user_id'=>1,

            'reporting'=>$value,
        ]);

    });

   return redirect('/')->with('success', 'Action was successful');
}

This will produce the data in this format:

user_id || reporting
1           Salman
1           Ankur  

Note: Tested working correctly!

3 Comments

Error: Undefined offset: 2
Collection {#217 ▼ #items: array:1 [▼ 0 => "multiple" ] }
I think your html its different from what you are displaying in the question. Try using my way it will work 100%.

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.