Home > Blockchain >  count(): Parameter must be an array or an object that implements Countable (laravel getting error)
count(): Parameter must be an array or an object that implements Countable (laravel getting error)

Time:06-07

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.

CodePudding user response:

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

  • Related