0

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

5
  • Can you dd() your $items array after the foreach loop and post it? I have a feeling your $items array isn't associative and/or doesn't have the Subject_id key set. Commented Jun 27, 2018 at 3:57
  • i have updated the question, DD is working fine Commented Jun 27, 2018 at 4:03
  • I think he meant dd($items) Commented Jun 27, 2018 at 4:04
  • yeah i use dd($items ) its working fine Commented Jun 27, 2018 at 4:13
  • Are you want to save array in 1 field? Commented Jun 27, 2018 at 4:35

3 Answers 3

3

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.');
}
Sign up to request clarification or add additional context in comments.

5 Comments

this code work but i can only save 1 value at a time ..is it possible to make it array
Thank you bro it works, but there is a bit problem it will only save 1=1 value example student1=[subject1] if i try to save student1=[subject1,subject2,subject3] it doesn't work but thanks man ill just have to figure this out i really appreciate, you're answer is the only one working at the moment hehe
anytime bro. :) if you wanna store array in table use json_encode(array()). store the encoded value as a string in a column. the use json_decode(string) to again convert it into an array.
you really help me a lot bro.. thank you so much i wish i could repay your'e goodness and buy u a beer hehe..
glad to hear that it helped. it feels good after helping someone don't worry about repaying :)
1

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

1 Comment

Don't expect us to write code for you. We can only give you hints/help to come close to your code.
0

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

3 Comments

this code works but wrongly saving data. it won't follow the array assign to it example: lead_id[1,2,3,4,5] with subject_id[1]
Are you trying to see which students chooses how many subjects ? Student A = {Subject 1, subject 2} Student b={Subject 1. Subject 2, Subject 3} Student 3 ={Subject 3} is this what you want to achieve??
yes for example student 1 [subject1,subject2,subject3] is being assigned

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.