Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading