1

I have a form within a table that has different fields. I want to be able to store all the data once. I hve search Stack overflows, all the answer i got did not solve the issue.Hence, I have to post my own question. The View

<td><input type="text" name="sflt[]" value="{{ $r['fltno'] }}" readonly="readonly" class="form-control"/></td>
<td><input type="text" name="smodel[]" value="{{ $r['model']}}" readonly="readonly" class="form-control"/></td> 
<td><input type="text" name="sengine[]" value="{{ $r['engine_type'] }}" readonly="readonly" class="form-control"/></td> 
<td><input type="text" name="sloc[]" value="{{ $r['location'] }}" readonly="readonly" class="form-control"/></td> 
<td><input type="text" name="sstye[]" value="{{ $sty }}" readonly="readonly" class="form-control"/></td> 
<td><input type="text" name="snsvr[]" value="{{ $nsvr}}" readonly="readonly" class="form-control"/></td> 

Controller

          $data = [];
          **//Get the input variables**
          $fltno= $request['sflt'];
          $model = $request['smodel'];
          $engine = $request['sengine'];
          $loc  = $request['sloc'];

// Store each varaibles as you fetch into the empty array

      foreach($fltno as $fltno)
         {
      $data[] = [
                'fltno'=>$request['sflt'],
                'model'=>$request['smodel'],
                'engine'=>$request['sengine'],
                loc'=>$request['sloc'],
               'serviceType'=>$request['sstye'],
               'nextSvr'=> $request['snsvr'] 
                ];
        }

       ModelName::insert($data); 

When I execute this, it throws error of: Invalid argument supplied for foreach()

Please, what am i doing wrong or what is the best way to insert all these data at once into the Database

7
  • Hi your code is not clear. Why you using $request in foreach ? Can you post exact code? Commented Aug 24, 2018 at 10:19
  • foreach($fltno as $fltno) don't use the same names here? loc' add a starting ' here? Have you dd($fltno) to see if it has the desired values? Why are all your fields named with []? Commented Aug 24, 2018 at 10:19
  • can you explain your code in proper format? @Dave Commented Aug 24, 2018 at 10:21
  • @amku91: I used it to get all the data from the array.Please, help if i m wrong, that is why i posted what i think was right, so i need guidance Commented Aug 24, 2018 at 10:23
  • @DhruvRaval: What i want to achieve is this: the form input name is an array of data like 150 rows, so i want to insert it once into the database when i clicked a save button. Commented Aug 24, 2018 at 10:24

4 Answers 4

4

Hi Dave I think you want to do something like this

$data = $request->all();
$finalArray = array();
foreach($data as $key=>$value){
   array_push($finalArray, array(
                'fltno'=>$value['sflt'],
                'model'=>$value['smodel'],
                'engine'=>$value['sengine'],
                'loc'=>$value['sloc'],
                'serviceType'=>$value['sstye'],
                'nextSvr'=> $value['snsvr'] )
   );
});

Model::insert($finalArray);

This will submit all data. I am assuming you are getting array in request with these keys.

Hope this will help.

Sign up to request clarification or add additional context in comments.

6 Comments

let me try this soltuion
Let me try this as well
sure. try this and let me know
This is the error is showing now: Illegal string offset 'sflt'
do print_r($value); die; in starting of foreach and tell whats coming @Dave
|
2

I think you are making mistake when retrieve the request body

$fltno = $request->input('sflt');

for the foreach I think you can do this

$fltnos = $request->input('sflt');

foreach($fltnos as $fltno) {
}

If you are using ORM

$fltnos = $request->input('sflt');
$models = $request->input('smodel');
$engines = $request->input('sengine');
$locs  = $request->input('sloc');

foreach($fltnos as $key => $fltno) {
    $modelName = new ModelName;
    $modelName->fltno = $fltno;
    $modelName->model = isset($models[$key]) ? $models[$key] : ''; // this is just workaround, you must make sure all field have same length if want to do this
    ...
    $modelName->save()
}

3 Comments

let me try please
sorry, you can use the insert method too. I usually use the save one by one style
i think you can do dd($fltnos) to see what's inside $fltnos
1

this is my working example you can implement in your code.

$competition_que = [];
        foreach ($input['options'] as $key => $value) {
            $competition_que[$key]['competition_id'] = $request->competition_id;
            $competition_que[$key]['question_id'] = $question->id;
            $competition_que[$key]['options'] = $input['options'][$key];
            $competition_que[$key]['created_at'] = Carbon::now();
            $competition_que[$key]['updated_at'] = Carbon::now();
            $competition_que[$key]['is_answer'] = ($key == $input['is_answer']) ? 'true' : 'false';
        }

//        return $competition_que;
        CompetitionAnswer::insert($competition_que);

4 Comments

please, is options a field in the form named like this: options[] ?
is options a name in the form like options[]? or what?
if your data from database then take id in hidden field. that is easy to use in for loop. @Dave
1

I think you can do it this way

$fltnos = $request->input('sflt', []); // second parameter is to set default value 
$models = $request->input('smodel', []);
$engines = $request->input('sengine', []);
$locs  = $request->input('sloc', []);

also you need to validate your bulk data prior to save

you can do it like this

public function store(Request $request)
{
    $validatedData = $request->validate([
        'sflt.*' => 'email|unique:users',
        'smodel.*' => 'required'
    ]);


if(validatedData) {

foreach($fltnos as $key => $fltno) {
    $modelName = new ModelName;
    $modelName->fltno = $fltno;
    ...
    $modelName->save()
}

}

}

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.