0

To save 1 data is like this. Should I loop the ->save(); part so I can make 2 or more different columns for each record?

$rec = new Record;

$rec->record_id = $id; // multiple ids
$rec->record_name = $name // multiple names

$rec->save();
Peter
  • 3
  • 3
  • 1
    Please check https://codingdriver.com/how-to-save-multiple-records-in-database-using-laravel.html – Aqib Javed Jul 28 '22 at 08:35
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Gary Houbre Jul 28 '22 at 10:03

2 Answers2

0
$rec = [
    ['record_id'=>$id, 'record_name'=>$name],
    ['record_id'=>$id2, 'record_name'=>$name2],
    //...
];

Record::insert($rec);

Same Question

Vüsal Hüseynli
  • 889
  • 1
  • 4
  • 16
0
public function store(Request $request)
{
    $data = $request->all();
    $mark = new Record();
    $mark->out_of = $data['out_of'];
    if ($data['student_id']) {
        foreach ($data['student_id'] as $row => $value) {
            $data1 = array(
                'out_of' => $mark->out_of,
                'student_id' => $data['student_id'][$row],
                'guardian_id' => $data['guardian_id'][$row],
                'marks_obtained' => $data['marks_obtained'][$row],
                'comment' => $data['comment'][$row],
            );
            Record::create($data1);
        }
    }
    return redirect('/home')->with('success', 'Record Added Successfully');
}

in your view

<form action="/record/store" method="post">
  @csrf
   <input type="text" name="student_id">
   <input type="text" name="guardian_id[]">
   <input type="text" name="marks_obtained[]">
   <input type="text" name="out_of">
   <input type="text" name="comment[]">
   <button type="submit">Post</button>

</form>
Nana
  • 36
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 30 '22 at 11:11