0

I have a form for adding cheques to a system. The form has two parts; part one is for the common info i.e the customer details and part two is for the unique details i.e cheque details. Multiple cheques can be added for the same user by clicking an Add button which generates duplicate inputs for the form. While saving the data on mysql database using Laravel, i got the error

"Undefined offset: 1"

Here is my laravel controller :

public function store(Request $request)
{
  $customer_name = $request->customer_name;
  $cheque_number=$request->cheque_number;
  $count = count($cheque_number);

    for($i = 0; $i < $count; $i++){
        $objModel = new Cheque();
        $objModel->customer_name = $customer_name[$i];
        $objModel->cheque_number = $cheque_number[$i];
        $objModel->save();
    }
}

My main challenge is each cheque number should be saved on a new row, but with the same customer name which is provided only once.

5
  • customer name contain single value or mulitple ? Commented Feb 20, 2019 at 12:15
  • the customer name is a single value while the cheque number can have more than one value. different cheque numbers should be stored in different db rows but with the same customer name Commented Feb 20, 2019 at 12:16
  • Can you show your form that sent data to controller ? Commented Feb 20, 2019 at 12:17
  • @Shibon No. That looks like a different question. Commented Feb 20, 2019 at 12:28
  • stackoverflow.com/questions/43036183/… Commented Feb 20, 2019 at 12:30

3 Answers 3

1

try this one

public function store(Request $request)
{
  $customer_name = $request->customer_name;
  $cheque_number = $request->cheque_number;
  $count = count($cheque_number);

    for($i = 0; $i < $count; $i++){
       if(isset($cheque_number[$i])) {  //for check value is set or not..
          $objModel = new Cheque();
          $objModel->customer_name = $customer_name;  //same name
          $objModel->cheque_number = $cheque_number[$i]; //different number
          $objModel->save();  
       }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
public function store(Request $request){
 $customer_name = $request->get('customer_name');
 $cheque_number = $request->get('cheque_number');
 $count = count($cheque_number);

 for($i = 0; $i < $count; $i++){
  if(isset($cheque_number[$i])){
   $objModel = new Cheque();
          $objModel->customer_name = $customer_name;  //same name
          $objModel->cheque_number = $cheque_number[$i]; //different number
          $objModel->save(); 
  }
 }
}

Comments

-1

You should try this:

public function store(Request $request)
{
  $customer_name = $request->customer_name;
  $cheque_number = $request->cheque_number;
  $count = count($cheque_number);

    for($i = 0; $i < $count; $i++){
       if(isset($cheque_number[$i])) {  
          $objModel = new Cheque();
          $objModel->customer_name = $customer_name;  //same name
          $objModel->cheque_number = $cheque_number[$i]; //different number
          $objModel->save();  
       }
    }
}

1 Comment

@Vic: Glad to help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.