Grace
Grace

Reputation: 299

Laravel store value from for-each loop and passed it as array

I have two for-each loop inside my create view [Subject_id, Lead_id], I want to store the for-each value into my database using array approach, and I couldn't make it work can anyone amend my codes or point me to a proper way thanks.

Controller:

public function store(Request $request)
{
    //
    $input = $request->all();
    $items = array(['Subject_id','Lead_id']);
    foreach($input as $inputs) {
        $items[] = $inputs;
    }
    $scores = new Score();
    $scores->Subject_id=$items['Subject_id'];
    $scores->Lead_id=$items['Lead_id'];
    $scores->save();
    dd($request->all());
    return redirect()->route('scores.create')->with('notif', 'Success.');
}

this is the message:enter image description here

create view

@foreach ($leads as $lead)
    <tr>
        <td>{{ $lead->student_name }}</td>
        <td><input type="checkbox" class="checkbox" name="Lead_id[]"  value="{{ $lead->id }}"></td>
    </tr>
@endforeach
@foreach($subjects as $subject)
 <label >
    <input type="checkbox" name="Subject_id[]" value="{{ $subject->id }}">
    {{ $subject->subject_name }}
 </label>
 @endforeach

DD Result:enter image description here

Upvotes: 0

Views: 15193

Answers (3)

Jaskaran Singh
Jaskaran Singh

Reputation: 914

Try this code in your controller

public function store(Request $request)
{
  $data = $request->all();

  $leads = $data['Lead_id'];

  $subject_ids = $data['Subject_id'];

  //insert using foreach loop
  foreach($leads as $key => $input) {
    $scores = new Score();
    $scores->Subject_id = isset($leads[$key]) ? $leads[$key] : ''; //add a default value here
    $scores->Lead_id = isset($subject_ids[$key]) ? $subject_ids[$key] : ''; //add a default value here
    $scores->save();
  }


  //insert using array at once
  $rows = [];
  foreach($leads as $key => $input) {
    array_push($rows, [
      'Subject_id' => isset($leads[$key]) ? $leads[$key] : '', //add a default value here
      'Lead_id' => isset($subject_ids[$key]) ? $subject_ids[$key] : '' //add a default value here
    ]);
  }
  Score::insert($rows);

  return redirect()->route('scores.create')->with('notif', 'Success.');
}

Upvotes: 3

Faizan Fayaz
Faizan Fayaz

Reputation: 568

Every time creating an instance of model in foreach loop in not an efficient way. You can do something like this

   foreach($input as $inputs) {
     $dataArray[] = [
    'Subject_id' => $inputs['Subject_id'],
    'Lead_id' => $inputs['Lead_id'],
    ];    
    }
DB::table('Score')->insert($dataArray);

You can manipulate the data as you want.

with this code i have thisenter image description here

Upvotes: 1

Ranjan Adhikari
Ranjan Adhikari

Reputation: 261

Update your blade

    @foreach ($leads as $lead)
    {{ $lead->student_name }}
        @foreach($subjects as $subject)
          <input type="checkbox" name="Subject_id[$lead->id][]" value="{{ $subject->id }}">
          {{ $subject->subject_name }}
        @endforeach
   @endforeach

This as your controller

   public function store(Request $request)
    {
        //
    $subjects = $request->get('Subject_id');

       foreach($subjects as $key=>$oneLeadOptions) {

           foreach($oneLeadOptions as $option){
                  $scores = new Score();
                  $scores->Subject_id  = $option;
                  $scores->Lead_id = $key;
                  $scores->save();

           }
       }
    //To to other redirects logic here
    }

try this

Upvotes: 0

Related Questions