-1

I am using eloquent model and there is json request which contains array and i am prossessing it using foreach like folloeing:

public function save(Request $request){
  $i = 0;
  $flag = 1; 
  foreach ($request->all() as $record) {
    if($flag){
      $flag = 0;
      continue;
    }
    else{
      $user = \App\barcodedb::create($record);
    }
  }
}

is it the correct way to save the record from array?

Komal
  • 2,716
  • 3
  • 24
  • 32
SamD
  • 185
  • 5
  • 22

2 Answers2

0

Try like this

$request_data = $request->all();
$data = json_decode($request_data['data']);

Store array in $data and use it

Komal
  • 2,716
  • 3
  • 24
  • 32
  • But I dont want this. what i want is to extract one array from large json and save it directly in database table like i did above – SamD Oct 15 '16 at 11:03
  • 1
    What you answered me is already i have implemented successfully. – SamD Oct 15 '16 at 11:08
0

There is no need for a foreach loop. After the JSON is converted to an array, you can just parse this array as the only argument in the insert method, like this:

App\barcodedb\insert($data);

Laravel will automatically recognize such an array as an array containing multiple new records.

See this answer: https://stackoverflow.com/a/13595393

Community
  • 1
  • 1
Jan Willem
  • 1,280
  • 1
  • 9
  • 9