0

I want to enter an multiple field entered data in table with for loop but i am getting an error in the post method.

error is

count(): Parameter must be an array or an object that implements Countable

controller code :-

$degree = $request->degree;
            for($i=0;$i<count($degree);$i++){
                $edu = new education;
                $edu->degree = $request->degree[i];
                $edu->clg = $request->clg[i];
                $edu->yoc = $request->yoc[i];
                $edu->save();
            }

so, please suggest me what i can do.

5
  • dd($degree) is it array or not ? Commented Jun 7, 2022 at 6:18
  • yes it is array enterd dynamically added value it is Commented Jun 7, 2022 at 6:19
  • what is the result of dd($degree) ? Commented Jun 7, 2022 at 6:22
  • array result enterd dynamically value will shown Commented Jun 7, 2022 at 6:23
  • double check your $request->degree : php.net/manual/en/function.count.php , with php version > 7.2 Commented Jun 7, 2022 at 6:23

1 Answer 1

1

Here not at all big problem,
you can not use count for the one value the array is required for that i think that you have not been enterd dynamically many values it can be 0

so replace code in your controller:-

$degree = $request->degree;
        if($degree > 0)
        {
            for($i=0;$i<count($degree);$i++){
                $edu = new education;
                $edu->degree = $request->degree[i];
                $edu->clg = $request->clg[i];
                $edu->yoc = $request->yoc[i];
                $edu->save();
            }
        }

here i have used $degree if its value is greater then 0 that means if it has value count grater then one then you can only go for loop and add value to database otherwise it will be not go in for loop

Sign up to request clarification or add additional context in comments.

Comments

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.