'Laravel request is an array but not recognized by count()
I am passing an array into my controller via axios.post request. I am trying to get the length of the $request array that I am passing to the controller. However, I keep recieving a "Parameter must be an array or an object that implements Countable" error.
Here is what my array looks like:
array (
0 =>
array (
'text' => 'It is this',
'question_id' => 98,
),
1 =>
array (
'text' => 'And it is that',
'question_id' => 98,
),
2 =>
array (
'text' => 'Also a little bit of this',
'question_id' => 98,
),
Here is what I have tried:
$count = sizeof($request));
$count = $request->length;
$count = count($request);
The only thing that has had even a little bit of success is doing:
$count = count($request[0])
This returns 2, which is for the elements inside the first array. It counts text, and question_id. While this is good progress this is not what I want
What I would like to see happen is to have the length of the entire $request object. In the example I gave above, I would like to either receive 2, (the end of 0,1,2) or 3 (the count of 0,1,2).
Solution 1:[1]
What about:
count($request->all());
Solution 2:[2]
In as much as the accepted answer will count the number of items in the requests, it will not count the number of items in a single request request item say C. If you are sure that C is an array and C is part of the request then doing count($request->C) not e that count here is a regular PHP function
Solution 3:[3]
try ;
count($request->all());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nakov |
| Solution 2 | The Only Smart Boy |
| Solution 3 |
